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);
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.