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:
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:
class C {
final String? s;
C(String this.s);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.