final_not_initialized
The final variable '{0}' must be initialized.
Description
#The analyzer produces this diagnostic when a final field or variable isn't initialized.
Example
#The following code produces this diagnostic because x
doesn't have an
initializer:
dart
final x;
Common fixes
#For variables and static fields, you can add an initializer:
dart
final x = 0;
For instance fields, you can add an initializer as shown in the previous example, or you can initialize the field in every constructor. You can initialize the field by using an initializing formal parameter:
dart
class C {
final int x;
C(this.x);
}
You can also initialize the field by using an initializer in the constructor:
dart
class C {
final int x;
C(int y) : x = y * 2;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。