type_check_with_null
Details about the 'type_check_with_null' diagnostic produced by the Dart analyzer.
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.10.3 版本报告页面问题.