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
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();
}class A { final a = const []; final List<int> b; const B() : b = const [];class C { final int x = 1, y = 2;class C { final int x = 1, y = 2; final String label; const D(this.value) : label = 'fixed';class E { final String name = 'e';mixin class F { final int f = 0;Valid
// 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.
{
"linter": {
"rules": {
"style": {
"avoid-field-initializers-in-const-classes": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-field-initializers-in-const-classes: <reason>More style rules
- avoid-final-parameters
avoid_final_parametersFlags parameters declared
final. - avoid-implementing-value-types
avoid_implementing_value_typesFlags
implementsof a class that overrides==. - avoid-init-to-null
avoid_init_to_nullrecommendedFlags explicit
= nullinitializers on declarations that already default to null. - avoid-js-rounded-ints
avoid_js_rounded_intsrecommendedAvoid integer literals that JavaScript would round.
- avoid-late-keyword
avoid-late-keywordFlags use of the
latekeyword on variables and fields. - avoid-multiple-declarations-per-line
avoid_multiple_declarations_per_linerecommendedFlags a variable declaration that declares more than one variable.
- avoid-non-null-assertion
avoid-non-null-assertionFlags the null-assertion operator
!except on a resolvedMapindex. - avoid-null-checks-in-equality-operators
avoid_null_checks_in_equality_operatorsrecommendedFlags null checks of the parameter inside a custom
operator ==.