Maaz Zindani

Loading the index…

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

Dart says a value can't be returned because of the declared return type

Errorreturn_of_invalid_type

Short answer

The analyzer reports return_of_invalid_type when a function returns a value that does not fit its declared return type. Either convert the value to the declared type, or change the declared type to match. In async code, the declared return type must be Future of the value type.

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

Fix it fast

  1. 1Find out which cause you have

    Read the two types named in the message and the line it points to. The rest of this page is about which one of the two is wrong.

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

    1. 1The function body uses async or await but the return type is int, String or a model classAn async function is declared with a plain typeFix
    2. 2No async keyword is involved, for example a function declared to return String that returns an intThe returned value has a different type than the signature promisesFix

What does the error look like?

  • return_of_invalid_type
  • A value of type can't be returned from method because it has a return type of

Cause 1 of 2

An async function is declared with a plain type

1Check

Look at the signature. A function marked async hands its value back inside a future.

grep -rnE "^\s*(int|String|bool|List<.*>) \w+\(.*\) async" lib

2Fix

Dart's async guide says to wrap the declared return type in Future of the value's type, and the callers then await the result.

// before
String loadName() async { return 'Maaz'; }

// after
Future<String> loadName() async { return 'Maaz'; }

final name = await loadName();

3Verify

Run the analyzer again; the error on that function is gone.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 2 of 2

The returned value has a different type than the signature promises

1Check

Compare the declared type with the type of the expression after return.

dart analyze

2Fix

Dart's own diagnostic page offers two fixes: replace the returned value with one of the right type, or change the return type to the type of the value.

// convert the value
String f() => 3.toString();

// or change the declared type
int f() => 3;

3Verify

Run the analyzer; the diagnostic is no longer reported.

dart analyze

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Write the return type first and let the analyzer check each return statement against it.
  • Mark every async method's type as Future<T> or Future<void> in the same edit that adds async.
  • Run dart analyze in CI so type mismatches cannot be merged.

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.