Dart Analyze logoDart Analyze/
DRT-W1648

Tests for non-null should be done with '!= null'. Tests for null should be done with '== null'DRT-W1648

Major severityMajor
Bug Risk categoryBug Risk

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);
}