null_check_always_fails
This null-check will always throw an exception because the expression will always evaluate to 'null'.
Description
#The analyzer produces this diagnostic when the null check operator (!
) is used on an expression whose value can only be null
. In such a case the operator always throws an exception, which likely isn't the intended behavior.
Example
#The following code produces this diagnostic because the function g
will always return null
, which means that the null check in f
will always throw:
void f() {
g()!;
}
Null g() => null;
Common fixes
#If you intend to always throw an exception, then replace the null check with an explicit throw
expression to make the intent more clear:
void f() {
g();
throw TypeError();
}
Null g() => null;
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.