avoid_field_initializers_in_const_classes
Avoid field initializers in const classes.
Details
#AVOID field initializers in const classes.
Instead of final x = const expr;
, you should write get x => const expr;
and not allocate a useless field. As of April 2018 this is true for the VM, but not for code that will be compiled to JS.
BAD:
class A {
final a = const [];
const A();
}
GOOD:
class A {
get a => const [];
const A();
}
Enable
#To enable the avoid_field_initializers_in_const_classes
rule, add avoid_field_initializers_in_const_classes
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- avoid_field_initializers_in_const_classes
If you're instead using the YAML map syntax to configure linter rules, add avoid_field_initializers_in_const_classes: true
under linter > rules:
linter:
rules:
avoid_field_initializers_in_const_classes: 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.