return_ of_ invalid_ type
A value of type '{0}' can't be returned from the constructor '{1}' because it has a return type of '{2}'.
A value of type '{0}' can't be returned from the function '{1}' because it has a return type of '{2}'.
A value of type '{0}' can't be returned from the method '{1}' because it has a return type of '{2}'.
Description
#The analyzer produces this diagnostic when a method or function returns a value whose type isn't assignable to the declared return type.
Example
#
The following code produces this diagnostic because
f
has a return type
of
String
but is returning an
int
:
String f() => 3;
Common fixes
#If the return type is correct, then replace the value being returned with a value of the correct type, possibly by converting the existing value:
String f() => 3.toString();
If the value is correct, then change the return type to match:
int f() => 3;
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.