Dart error: the body might complete normally and return null for a non-nullable type
The body might complete normally, causing 'null' to be returned, but the return type, 'int', is a potentially non-nullable type.Short answer
Dart found a path through your function or callback that reaches the closing brace without a return. Since null safety, a non-nullable return type cannot get the implicit null that older Dart added. Return a value on every path, throw where the path should never run, or change the return type to nullable.
Fix it fast
1Find out which cause you have
Run the analyzer. It prints the file and line of the function or closure with the open path, so you know which body to read.
dart analyze2Match what you see, then jump to its fix
- 1
The error points at a function whose last statement is not a return, often after an if with no elseAn if or switch branch has no returnFix - 2
All real cases are handled above, and the leftover path is a case you consider impossibleThe end of the function should never be reachedFix - 3
The function or callback is meant to return nothing for some inputs, such as a validator or a lookupReturning null is intended, but the type does not allow itFix
- 1
What does the error look like?
The body might complete normally, causing 'null' to be returned, but the return type, 'int', is a potentially non-nullable type.body_might_complete_normally
Cause 1 of 3
An if or switch branch has no return
1Check
Read the flagged body from top to bottom and find every way execution can leave it. Any branch that falls out of the closing brace is the problem.
int score(int n) {
if (n > 0) {
return n * 2;
}
// n <= 0 reaches the end here: flagged
}2Fix
Add a return at the end of the body, or an else branch that returns. Every path must end in a return that gives a value.
int score(int n) {
if (n > 0) {
return n * 2;
}
return 0;
}3Verify
Run the analyzer again; the diagnostic for that line should be gone.
dart analyzeConfirmed by dart.dev
Cause 2 of 3
The end of the function should never be reached
1Check
Ask what should happen if the code did get to the end. If the honest answer is 'a bug', the path is not a normal return.
dart analyze lib/2Fix
Throw at the end instead of inventing a value. A throw ends the path, so the analyzer no longer sees a way to fall through.
String label(int level) {
if (level == 1) return 'low';
if (level == 2) return 'high';
throw ArgumentError('Unknown level: $level');
}3Verify
Analyze, then call the function with an unexpected value in a test and confirm it throws.
dart analyzeConfirmed by dart.dev
Cause 3 of 3
Returning null is intended, but the type does not allow it
1Check
Look at the declared return type. A type like String or int cannot hold null. Without a return, Dart implicitly appends return null, which is what the error complains about.
String validate(String v) { // type does not allow null
if (v.isEmpty) return 'Required';
}2Fix
Make the return type nullable and return null explicitly on the path that has no value. Callers must then handle null.
String? validate(String v) {
if (v.isEmpty) return 'Required';
return null;
}3Verify
Analyze again and check that each caller handles the nullable result.
dart analyzeConfirmed by dart.dev
How do I stop it coming back?
- End every non-void function with an explicit return or throw, so a new branch added later cannot fall through.
- Give callbacks such as builders and mappers a block body only when every path returns; otherwise use an expression body.
- Run dart analyze in CI so the diagnostic is caught before a build.
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
Dart error: the parameter can't have a value of 'null' because of its type
The parameter 'title' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
An optional parameter that is not passed gets the value null.
4 causes, each with a fixMobile 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 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