Skip to main content

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;
}