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);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.