prefer-initializing-formals
Same as prefer_initializing_formals in Dart lints
- Group
- Style
- Path
lint/style/prefer-initializing-formals- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 372 of 512
Prefer initializing formals when a parameter is copied directly into its field.
An initializing formal makes field initialization explicit in the constructor
signature: replace Example(int value) : value = value; with
Example(this.value);. The rule also recognizes direct assignments in a
constructor body, including primary constructors.
Suggestions preserve the parameter's name and require its type to be a subtype of the field's type. Inherited fields, setters, static fields, and parameters referenced elsewhere in the constructor are excluded. Private fields are suggested only for named parameters when private named parameters are available.
Invalid
// Parameters copied verbatim into same-named fields.
class A {
int x;
A(int x) : x = x;
}
class B {
int y;
B(int y) { this.y = y; }
}
class C {
int a, b;
C(int a, int b) : a = a, b = b;
}
class D {
String name;
D(String name) : name = name;
}
class E {
int v;
E(int v) { this.v = v; }
}
class PrivateNamed {
int _value;
PrivateNamed({required int value}) : _value = value;
}
class Primary(int value) {
final int value;
this : value = value;
}
class ParenthesizedBody {
int value;
ParenthesizedBody(int value) { this.value = (value); }
}
class Subtype {
num value;
Subtype(int value) : value = value;
}
class Compound {
int value = 0;
Compound(int value) { this.value += value; }
} int x; A(int x) : x = x; int y; B(int y) { this.y = y; } int a, b; C(int a, int b) : a = a, b = b; int a, b; C(int a, int b) : a = a, b = b; String name; D(String name) : name = name; int v; E(int v) { this.v = v; } int _value; PrivateNamed({required int value}) : _value = value; final int value; this : value = value; int value; ParenthesizedBody(int value) { this.value = (value); } num value; Subtype(int value) : value = value; int value = 0; Compound(int value) { this.value += value; }Valid
// Uses initializing formals, or assignments that cannot become one.
class A {
int x;
A(this.x);
}
class C {
int a;
C(int a) : a = a + 1; // not a verbatim copy
}
class D {
int v;
D(this.v);
}
class G {
int p, q;
G(int v) : p = v, q = v; // one parameter, two fields — no initializing formal
}
class H {
int r;
H({required int value}) : r = value; // renaming a named parameter breaks callers
}
class F {
int z;
F(this.z) { print(z); }
}
class DifferentName {
int value;
DifferentName(int input) : value = input;
}
class PrivatePositional {
int _value;
PrivatePositional(int value) : _value = value;
}
class Parent {
int value = 0;
}
class Inherited extends Parent {
Inherited(int value) { this.value = value; }
}
class Setter {
set value(int value) {}
Setter(int value) { this.value = value; }
}
class Reused {
int value;
Reused(int value) : value = value { print(value); }
}
class DynamicCopy {
int value;
DynamicCopy(dynamic value) : value = value;
}
class StaticField {
static int value = 0;
StaticField(int value) { StaticField.value = value; }
}
class ParenthesizedInitializer {
int value;
ParenthesizedInitializer(int value) : value = (value);
}
class InitializerReuse {
int value;
int other;
InitializerReuse(int value) : value = value, other = value;
}
class ShadowedParameter {
late int value;
ShadowedParameter(int value) : assert(value > 0) {
final value = 0;
this.value = value;
}
}
class DeclaringPrimary(var int value) {
int other;
this : other = value {
this.value = value;
}
}Configure prefer-initializing-formals
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-initializing-formals": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-initializing-formals: <reason>More style rules
- prefer-inlined-adds
prefer_inlined_addsrecommendedFlags an
addcascade on a list literal whose element could be written inline. - prefer-int-literals
prefer_int_literalsFlags a whole-number double literal, such as
1.0, where the context type isdoubleso the int literal1means the same value. - prefer-interpolation-to-compose-strings
prefer_interpolation_to_compose_stringsrecommendedPrefers string interpolation to composing strings with
+. - prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck. - prefer-is-not-empty
prefer_is_not_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isNotEmptycheck. - prefer-is-not-operator
prefer_is_not_operatorrecommendedFlags a negated type test
!(x is T)that should use theis!operator. - prefer-iterable-of
prefer-iterable-ofrecommendedFlags
.fromcollection constructors in favor of the.ofconstructor. - prefer-iterable-where-type
prefer_iterable_whereTyperecommendedFlags
.where((e) => e is T), which.whereType<T>()expresses directly.