Future
Dart 是一门单线程编程语言
- Future 对象封装了Dart 的异步操作,它有未完成(uncompleted)和已完成(completed)两种状态。
- completed状态也有两种:一种是代表操作成功,返回结果;另一种代表操作失败,返回错误。
1.1 then
void main(List<String> arguments) {
print('start');
Future f = getNetwort();
print(f.then((value) => print(value)).catchError((onError){
print(onError);
}));
print('end');
}
Future<String> getNetwort(){
return Future((){
sleep(Duration(seconds: 2));
// throw Exception('403');
return "yh";
});
}
//start
//Instance of 'Future<void>'
//end
//yh
//Exception: 403
1.2 async 和 await