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:
final x;
Common fixes
#For variables and static fields, you can add an initializer:
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:
class C {
final int x;
C(this.x);
}
You can also initialize the field by using an initializer in the constructor:
class C {
final int x;
C(int y) : x = y * 2;
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.