跳转至主要内容

return_of_invalid_type

A value of type '{0}' can't be returned from the constructor '{2}' because it has a return type of '{1}'.

A value of type '{0}' can't be returned from the function '{2}' because it has a return type of '{1}'.

A value of type '{0}' can't be returned from the method '{2}' because it has a return type of '{1}'.

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:

dart
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:

dart
String f() => 3.toString();

If the value is correct, then change the return type to match:

dart
int f() => 3;