Skip to main content

prefer_initializing_formals

Use an initializing formal to assign a parameter to a field.

Description

#

The analyzer produces this diagnostic when a constructor parameter is used to initialize a field without modification.

Example

#

The following code produces this diagnostic because the parameter c is only used to set the field c:

dart
class C {
  int c;

  C(int c) : this.c = c;
}

Common fixes

#

Use an initializing formal parameter to initialize the field:

dart
class C {
  int c;

  C(this.c);
}