Maaz Zindani

Loading the index…

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

Dart error: the body might complete normally and return null for a non-nullable type

ErrorThe 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.

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

Fix it fast

  1. 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 analyze
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2All real cases are handled above, and the leftover path is a case you consider impossibleThe end of the function should never be reachedFix
    3. 3The 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

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 analyze

Confirmed by dart.dev

Did this fix it?

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 analyze

Confirmed by dart.dev

Did this fix it?

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 analyze

Confirmed by dart.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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