Dart Analyze

Dart Analyze

Community Analyzer

Avoid field initializers in const classes DRT-W1053

Anti-pattern
Major

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();
}