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
:
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:
class A {
int x = 0;
m([int x = 0]) {
this.x = x;
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.