not_initialized_non_nullable_instance_field
Non-nullable instance field '{0}' must be initialized.
Description
#The analyzer produces this diagnostic when a field is declared and has all these characteristics:
- Has a type that's potentially non-nullable
- Doesn't have an initializer
- Isn't marked as
late
Examples
#The following code produces this diagnostic because x
is implicitly initialized to null
when it isn't allowed to be null
:
class C {
int x;
}
Similarly, the following code produces this diagnostic because x
is implicitly initialized to null
, when it isn't allowed to be null
, by one of the constructors, even though it's initialized by other constructors:
class C {
int x;
C(this.x);
C.n();
}
Common fixes
#If there's a reasonable default value for the field that's the same for all instances, then add an initializer expression:
class C {
int x = 0;
}
If the value of the field should be provided when an instance is created, then add a constructor that sets the value of the field or update an existing constructor:
class C {
int x;
C(this.x);
}
You can also mark the field as late
, which removes the diagnostic, but if the field isn't assigned a value before it's accessed, then it results in an exception being thrown at runtime. This approach should only be used if you're sure that the field will always be assigned before it's referenced.
class C {
late int x;
}
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.