Skip to main content

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:

Examples

#

The following code produces this diagnostic because x is implicitly initialized to null when it isn't allowed to be null:

dart
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:

dart
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:

dart
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:

dart
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.

dart
class C {
  late int x;
}