Dart warns that the left operand of ?? can't be null
dead_null_aware_expressionShort 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.
Fix it fast
1Find out which cause you have
Run the analyzer to list every place it reports, with the line numbers and the rule name.
dart analyze2Match what you see, then jump to its fix
- 1
dead_null_aware_expression on a line like color ?? Colors.grey.The left side is a non-nullable type Fix - 2
invalid_null_aware_operator, reported on a ?. or ?[ whose receiver cannot be null.?. or ?[ is used on a receiver that cannot be nullFix - 3
dead_null_aware_expression on a line like f ??= x where f is declared int.A ??= on a field that is never nullFix - 4
Removing ?? changes behaviour: callers do pass null, or the value is read from JSON.The type should be nullable, because null really can happenFix
- 1
What does the error look like?
dead_null_aware_expressioninvalid_null_aware_operatorThe 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 analyzeConfirmed by dart.dev
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 analyze2Fix
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 analyzeConfirmed by dart.dev
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 analyzeConfirmed by dart.dev
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 analyze2Fix
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 analyzeConfirmed by dart.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix