prefer_final_fields
The private field {0} could be 'final'.
Description
#The analyzer produces this diagnostic when a private field is only assigned one time. The field can be initialized in multiple constructors and still be flagged because only one of those constructors can ever run.
Example
#The following code produces this diagnostic because the field _f
is only assigned one time, in the field's initializer:
dart
class C {
int _f = 1;
int get f => _f;
}
Common fixes
#Mark the field final
:
dart
class C {
final int _f = 1;
int get f => _f;
}
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.