avoid-field-initializers-in-const-classes

Same as avoid_field_initializers_in_const_classes in Dart lints

Group
Style
Path
lint/style/avoid-field-initializers-in-const-classes
Default
Off, opt in
Origin
Dart lints
Rule
64 of 512

Flags field initializers in classes with a const constructor.

In a class that can be instantiated as a constant, a final instance field initialized to a fixed value — either at its declaration or in the initializer list of the class's only const constructor without using any constructor parameter — stores the same value in every instance. A getter expresses that without spending a field per object.

class A {
  final a = const [];         // flagged
  const A();
}

class B {
  final b;
  const B() : b = const [];   // flagged
}

class C {
  List get c => const [];     // preferred
  const C();
}

An initializer-list entry that reads a constructor parameter is allowed, and initializer lists are not checked when the class declares several constructors.

Invalid

example.dartdart
class A {
  final a = const [];
  const A();
}

class B {
  final List<int> b;
  const B() : b = const [];
}

class C {
  final int x = 1, y = 2;
  const C();
}

// Initializer-list entry that ignores every parameter.
class D {
  final int value;
  final String label;
  const D(this.value) : label = 'fixed';
}

// A field initializer is flagged even when another constructor is not const.
class E {
  final String name = 'e';
  const E();
  E.other();
}

mixin class F {
  final int f = 0;
  const F();
}
Fields in `const` classes should not have initializers.
1class A {
2 final a = const [];
Fields in `const` classes should not have initializers.
7 final List<int> b;
8 const B() : b = const [];
Fields in `const` classes should not have initializers.
11class C {
12 final int x = 1, y = 2;
Fields in `const` classes should not have initializers.
11class C {
12 final int x = 1, y = 2;
Fields in `const` classes should not have initializers.
19 final String label;
20 const D(this.value) : label = 'fixed';
Fields in `const` classes should not have initializers.
24class E {
25 final String name = 'e';
Fields in `const` classes should not have initializers.
30mixin class F {
31 final int f = 0;

Valid

example.dartdart
// A getter instead of a field.
class A {
  List get a => const [];
  const A();
}

// Initializer-list entry that uses a constructor parameter.
class B {
  final int b;
  const B(int value) : b = value * 2;
}

// No const constructor: field initializers are fine.
class C {
  final c = const [];
  C();
}

// Static fields are not per-instance.
class D {
  static final d = <int>[];
  static const e = 1;
  const D();
}

// Several constructors: the initializer list is not checked.
class E {
  final int e;
  const E() : e = 0;
  const E.named(this.e);
}

// Implicit default constructor is never const.
class F {
  final f = 1;
}

// Field formal parameters, no initializers.
class G {
  final int x;
  final int y;
  const G(this.x, {this.y = 0});
}

// Parameter referenced in a nested expression.
class H {
  final int next;
  const H(int first) : next = (first + 1) * 2;
}

Configure avoid-field-initializers-in-const-classes

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-field-initializers-in-const-classes": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-field-initializers-in-const-classes: <reason>