Skip to main content

field_initializer_outside_constructor

Field formal parameters can only be used in a constructor.

Initializing formal parameters can only be used in constructors.

Description

#

The analyzer produces this diagnostic when an initializing formal parameter is used in the parameter list for anything other than a constructor.

Example

#

The following code produces this diagnostic because the initializing formal parameter this.x is being used in the method m:

dart
class A {
  int x = 0;

  m([this.x = 0]) {}
}

Common fixes

#

Replace the initializing formal parameter with a normal parameter and assign the field within the body of the method:

dart
class A {
  int x = 0;

  m([int x = 0]) {
    this.x = x;
  }
}