prefer_const_literals_to_create_immutables
Prefer const literals as parameters of constructors on @immutable classes.
This rule is available as of Dart 2.0.
This rule has a quick fix available.
Details
#PREFER using const
for instantiating list, map and set literals used as parameters in immutable class instantiations.
BAD:
@immutable
class A {
A(this.v);
final v;
}
A a1 = new A([1]);
A a2 = new A({});
GOOD:
A a1 = new A(const [1]);
A a2 = new A(const {});
Usage
#To enable the prefer_const_literals_to_create_immutables
rule, add prefer_const_literals_to_create_immutables
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- prefer_const_literals_to_create_immutables
Unless stated otherwise, the documentation on this site reflects Dart 3.5.3. Page last updated on 2024-07-03. View source or report an issue.