null_ check_ on_ nullable_ type_ parameter
The null check operator shouldn't be used on a variable whose type is a potentially nullable type parameter.
Description
#
The analyzer produces this diagnostic when a null check operator is used
on a variable whose type is T?, where T is a type parameter that
allows the type argument to be nullable (either has no bound or has a
bound that is nullable).
Given a generic type parameter T which has a nullable bound, it is very
easy to introduce erroneous null checks when working with a variable of
type T?. Specifically, it is not uncommon to have T? x;
and want to
assert that x has been set to a valid value of type T. A common
mistake is to do so using x!. This is almost always incorrect, because
if T is a nullable type, x may validly hold null
as a value of type
T.
Example
#
The following code produces this diagnostic because t has the type T?
and T allows the type argument to be nullable (because it has no
extends clause):
T f<T>(T? t) => t!;
Common fixes
#Use the type parameter to cast the variable:
T f<T>(T? t) => t as T;
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.