目錄
目錄

Avoid field initializers in const classes.

This rule is available as of Dart 2.0.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