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:
dart
class A {
final a = const [];
const A();
}
GOOD:
dart
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:
analysis_options.yaml
yaml
linter:
rules:
- avoid_field_initializers_in_const_classes
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。