type_check_with_null
Tests for non-null should be done with '!= null'.
Tests for null should be done with '== null'.
Description
#The analyzer produces this diagnostic when there's a type check (using the
as
operator) where the type is Null
. There's only one value whose type
is Null
, so the code is both more readable and more performant when it
tests for null
explicitly.
Examples
#The following code produces this diagnostic because the code is testing to
see whether the value of s
is null
by using a type check:
void f(String? s) {
if (s is Null) {
return;
}
print(s);
}
The following code produces this diagnostic because the code is testing to
see whether the value of s
is something other than null
by using a type
check:
void f(String? s) {
if (s is! Null) {
print(s);
}
}
Common fixes
#Replace the type check with the equivalent comparison with null
:
void f(String? s) {
if (s == null) {
return;
}
print(s);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。