Incompatible return type in `Future.catchError` invocationDRT-W1437
The analyzer produces this diagnostic when an invocation of
Future.catchError
has an argument whose return type isn't compatible with
the type returned by the instance of Future
. At runtime, the method
catchError
attempts to return the value from the callback as the result
of the future, which results in another exception being thrown.
Examples
The following code produces this diagnostic because future
is declared to
return an int
while callback
is declared to return a String
, and
String
isn't a subtype of int
:
void f(Future<int> future, String Function(dynamic, StackTrace) callback) {
future.catchError(callback);
}
The following code produces this diagnostic because the closure being
passed to catchError
returns an int
while future
is declared to
return a String
:
void f(Future<String> future) {
future.catchError((error, stackTrace) => 3);
}
Common fixes
If the instance of Future
is declared correctly, then change the callback
to match:
void f(Future<int> future, int Function(dynamic, StackTrace) callback) {
future.catchError(callback);
}
If the declaration of the instance of Future
is wrong, then change it to
match the callback:
void f(Future<String> future, String Function(dynamic, StackTrace) callback) {
future.catchError(callback);
}