Contents

avoid_field_initializers_in_const_classes

Contents

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