Missing initialization for final instance fieldsDRT-W1371
The analyzer produces this diagnostic when a class defines one or more final instance fields without initializers and has at least one constructor that doesn't initialize those fields. All final instance fields must be initialized when the instance is created, either by the field's initializer or by the constructor.
Example
The following code produces this diagnostic:
class C {
final String value;
C();
}
Common fixes
If the value should be passed in to the constructor directly, then use an
initializing formal parameter to initialize the field value
:
class C {
final String value;
C(this.value);
}
If the value should be computed indirectly from a value provided by the caller, then add a parameter and include an initializer:
class C {
final String value;
C(Object o) : value = o.toString();
}
If the value of the field doesn't depend on values that can be passed to the constructor, then add an initializer for the field as part of the field declaration:
class C {
final String value = '';
C();
}
If the value of the field doesn't depend on values that can be passed to the constructor but different constructors need to initialize it to different values, then add an initializer for the field in the initializer list:
class C {
final String value;
C() : value = '';
C.named() : value = 'c';
}
However, if the value is the same for all instances, then consider using a static field instead of an instance field:
class C {
static const String value = '';
C();
}