dart: unnecessary_null_comparision


For the following code:

bool assertTest(int? n1, int? n2) {
  return (n1 == null) || (n1 != null && n2 != null);

there is a warning at n1 != null saying The operand can't be null, so the condition is always true.
Why does this warning show up? n1 is obviously nullable.


The boolean operation are lazy, it means that if you evaluate a || b and a is true, then b is not even evaluated.

In your case, if b = (n1 != null && n2 != null) is evaluated, it means a = (n1 == null) = false, which means n1 != null so the check n1 != null will always be true.

Answered By – Valentin Vignal

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *