field_initialized_in_parameter_and_initializer
Fields can't be initialized in both the parameter list and the initializers.
Description
#The analyzer produces this diagnostic when a field is initialized in both the parameter list and in the initializer list of a constructor.
Example
#The following code produces this diagnostic because the field f
is initialized both by an initializing formal parameter and in the initializer list:
class C {
int f;
C(this.f) : f = 0;
}
Common fixes
#If the field should be initialized by the parameter, then remove the initialization in the initializer list:
class C {
int f;
C(this.f);
}
If the field should be initialized in the initializer list and the parameter isn't needed, then remove the parameter:
class C {
int f;
C() : f = 0;
}
If the field should be initialized in the initializer list and the parameter is needed, then make it a normal parameter:
class C {
int f;
C(int g) : f = g * 2;
}
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.