Skip to main content

tighten_type_of_initializing_formals

Use a type annotation rather than 'assert' to enforce non-nullability.

Description

#

The analyzer produces this diagnostic when an assert is being used in the initializer list of a constructor to ensure that only a non-null value is being used to initialize a field.

Example

#

The following code produces this diagnostic because an assert is being used to catch an error that could be caught by the type system:

dart
class C {
  final String? s;

  C(this.s) : assert(s != null);
}

Common fixes

#

Remove the assert and add the non-nullable type before the initializing formal:

dart
class C {
  final String? s;

  C(String this.s);
}