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
recommended

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

example.dartdart
// 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; }
}
Use an initializing formal to assign a parameter to a field.
3 int x;
4 A(int x) : x = x;
Use an initializing formal to assign a parameter to a field.
8 int y;
9 B(int y) { this.y = y; }
Use an initializing formal to assign a parameter to a field.
13 int a, b;
14 C(int a, int b) : a = a, b = b;
Use an initializing formal to assign a parameter to a field.
13 int a, b;
14 C(int a, int b) : a = a, b = b;
Use an initializing formal to assign a parameter to a field.
18 String name;
19 D(String name) : name = name;
Use an initializing formal to assign a parameter to a field.
23 int v;
24 E(int v) { this.v = v; }
Use an initializing formal to assign a parameter to a field.
29 int _value;
30 PrivateNamed({required int value}) : _value = value;
Use an initializing formal to assign a parameter to a field.
34 final int value;
35 this : value = value;
Use an initializing formal to assign a parameter to a field.
39 int value;
40 ParenthesizedBody(int value) { this.value = (value); }
Use an initializing formal to assign a parameter to a field.
44 num value;
45 Subtype(int value) : value = value;
Use an initializing formal to assign a parameter to a field.
49 int value = 0;
50 Compound(int value) { this.value += value; }

Valid

example.dartdart
// 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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-initializing-formals": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-initializing-formals: <reason>
  • prefer-inlined-addsprefer_inlined_addsrecommended

    Flags an add cascade on a list literal whose element could be written inline.

  • prefer-int-literalsprefer_int_literals

    Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.

  • prefer-is-not-operatorprefer_is_not_operatorrecommended

    Flags a negated type test !(x is T) that should use the is! operator.

  • prefer-iterable-ofprefer-iterable-ofrecommended

    Flags .from collection constructors in favor of the .of constructor.

  • prefer-iterable-where-typeprefer_iterable_whereTyperecommended

    Flags .where((e) => e is T), which .whereType<T>() expresses directly.