Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Dart warns that the left operand of ?? can't be null

Errordead_null_aware_expression

Short answer

Dart is telling you the value on the left of ?? has a non-nullable type, so the right side can never run. Remove the operator, or make the left type nullable if null really can happen.

  • Applies to Dart 3, Flutter 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the analyzer to list every place it reports, with the line numbers and the rule name.

    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1dead_null_aware_expression on a line like color ?? Colors.grey.The left side is a non-nullable type Fix
    2. 2invalid_null_aware_operator, reported on a ?. or ?[ whose receiver cannot be null.?. or ?[ is used on a receiver that cannot be nullFix
    3. 3dead_null_aware_expression on a line like f ??= x where f is declared int.A ??= on a field that is never nullFix
    4. 4Removing ?? changes behaviour: callers do pass null, or the value is read from JSON.The type should be nullable, because null really can happenFix

What does the error look like?

  • dead_null_aware_expression
  • invalid_null_aware_operator
  • The left operand can't be null, so the right operand is never executed.

Cause 1 of 4

The left side is a non-nullable type

1Check

Hover over the left operand in the IDE and read its type. If it ends without a ?, null cannot occur.

dart analyze lib/

2Fix

Delete the ?? and its right operand. The Dart documentation lists this as the fix for the diagnostic.

// before
final c = widget.color ?? Colors.grey;
// after, when color is a non-nullable Color
final c = widget.color;

3Verify

dart analyze no longer reports the line.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 2 of 4

?. or ?[ is used on a receiver that cannot be null

1Check

Find ?. on values whose type has no ?, including a value cast with as.

dart analyze

2Fix

Replace ?. with . (and ?[ with [ ). If the return type should be nullable as a result, adjust it deliberately.

// before
int getLength(String s) => s?.length ?? 0;
// after
int getLength(String s) => s.length;

3Verify

Run the analyzer again.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 3 of 4

A ??= on a field that is never null

1Check

Look at the field's declaration. A field with a default value and no ? cannot be null.

grep -rn "??=" lib/

2Fix

Remove the assignment, or change it to a normal assignment inside an if that checks the real condition.

// before
f ??= x;
// after, when f is set by some other rule
if (f < 0) f = x;

3Verify

dart analyze no longer reports the line.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 4 of 4

The type should be nullable, because null really can happen

1Check

Check where the value comes from. Types are non-nullable by default; a check on a non-nullable type is reported as a warning.

dart analyze

2Fix

Add ? to the declared type so the analyzer sees null as possible, and keep the ?? default.

final Color? color;
final c = color ?? Colors.grey;

3Verify

The warning is gone and the default still applies.

dart analyze

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Run dart analyze in CI so new warnings are seen the day a package changes a type.
  • Declare a type nullable only where null is a real input.
  • Read the changelog of a package when an upgrade starts producing these warnings.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.