avoid_field_initializers_in_const_classes
Avoid field initializers in const classes.
This rule is available as of Dart 2.0.
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();
}
Usage
#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
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.