final_not_initialized_constructor
All final variables must be initialized, but '{0}' and '{1}' aren't.
All final variables must be initialized, but '{0}' isn't.
All final variables must be initialized, but '{0}', '{1}', and {2} others aren't.
Description
#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();
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。