prefer_const_constructors_in_immutables
Prefer declaring const
constructors on @immutable
classes.
Details
#PREFER declaring const
constructors on @immutable
classes.
If a class is immutable, it is usually a good idea to make its constructor a const
constructor.
BAD:
@immutable
class A {
final a;
A(this.a);
}
GOOD:
@immutable
class A {
final a;
const A(this.a);
}
Enable
#To enable the prefer_const_constructors_in_immutables
rule, add prefer_const_constructors_in_immutables
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- prefer_const_constructors_in_immutables
If you're instead using the YAML map syntax to configure linter rules, add prefer_const_constructors_in_immutables: true
under linter > rules:
linter:
rules:
prefer_const_constructors_in_immutables: true
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2025-01-27. View source or report an issue.