ex:
import 'dart:async';
void main(){
new Future(() => futrueTask())
.then((i) => 'abc$i')
.then((m) => print('$m'))
.then((_) => new Future.error('出错了'))
.whenComplete(() => print('whenComplete'))
.catchError((e) => print(e), test: (Object o)
{
print('test');
return true; //返回true,会被catchError捕获,返回false就不会被捕获,依旧会报异常
});
}
futrueTask(){
return 10;
}
输出:
D:\flutter\bin\cache\dart-sdk\bin\dart.exe --enable-asserts --enable-vm-service:41526 D:\Code\Flutter\FlutterHello\flutter_app\lib\async.dart
lib/async.dart: Warning: Interpreting this as package URI, 'package:flutterapp/async.dart'.
Observatory listening on http://127.0.0.1:41526/gFvDtcAIxZo=/
abc10
whenComplete
test
出错了
Process finished with exit code 0
如果上面的return true修改成return false,输出就是下面的:
D:\flutter\bin\cache\dart-sdk\bin\dart.exe --enable-asserts --enable-vm-service:41560 D:\Code\Flutter\FlutterHello\flutter_app\lib\async.dart
lib/async.dart: Warning: Interpreting this as package URI, 'package:flutterapp/async.dart'.
Observatory listening on http://127.0.0.1:41560/4hLf05l5PgM=/
abc10
Unhandled exception:
出错了
whenComplete
test
Process finished with exit code 255