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;
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。