Dart Analyze logoDart Analyze/
DRT-W1554

This null-check will always throw an exception because the expression will always evaluate to 'null'DRT-W1554

Major severityMajor
Bug Risk categoryBug Risk

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;