invalid_ return_ type_ for_ then
Details about the 'invalid_return_type_for_then' diagnostic produced by the Dart analyzer.
A value of type '{0}' can't be returned by the 'onError' handler because it must be assignable to '{1}', as required by 'Future.then'.
The return type '{0}' isn't assignable to '{1}', as required by 'Future.then'.
Description
#
The analyzer produces this diagnostic when the return type of
the
onError argument in Future.then is
incompatible with the return type of the
onValue argument. At runtime,
Future.then attempts to return the value from the
onError handler as the future's result, which
throws another exception.
Examples
#
The following code produces this diagnostic because the
onError handler returns a
String instead of an int:
void f(Future<int> future) {
future.then((_) => 0, onError: (e, st) => 'c');
}
The following code produces this diagnostic because the return
type of the
onError argument (cb) is
incompatible with the return type of onValue:
void f(Future<int> future, String Function(dynamic, StackTrace) cb) {
future.then<int>((_) => 1, onError: cb);
}
Common fixes
#
If the onValue handler returns the correct type,
then change the onError handler to match:
void f(Future<int> future) {
future.then((_) => 0, onError: (e, st) => -1);
}
If the onError handler is correct, then change
the onValue handler to match:
void f(Future<String> future) {
future.then((_) => 'a', onError: (e, st) => 'c');
}
If both handlers are correct, then change the future type to match:
void f(Future<String> future) {
future.then<Object>((_) => 0, onError: (e, st) => 'c');
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.