Dart Future 使用

Future

Dart 的异步对象,类似于 Javascript 中的 Promise

  1. Future 表示一个异步操作返回的结果;
  2. Future 是一个泛型类;
  3. Future 实现异步的原因是通过 dartevent loop,这里不过多赘述。

基本使用

import 'package:http/http.dart' as http;Future getIp() {final url = 'https://httpbin.org/ip';return http.get(url).then((res) {return res.body;});
}void main() {getIp().then((res) {print(res);}).catchError((err) {print(err);});
}

方法

1. then

通过 .then 的形式,获取 Future 对象执行成功后的结果。

  1. 第一个参数 onValue
void main() {testThen().then((res) {print(res); // 异步数据// 可以将数据处理后,继续 return 出去,实现链式调用return "加工后的${res}";}).then((res) {print(res); // 加工后的异步数据});
}// 返回一个 Future 对象,其中 String 可以省略,会被 dart 推断出来
Future testThen() {// return 一个 Future 对象return new Future(() {// 发送给 .then 的数据return "异步数据";});
}
  1. 第二个参数 onError
void main() {testThen().then((res) {// 不执行 onValue 的方式print(res);return "加工后的${res}";}, onError: (err) {// 接收到 Future 的错误print(err); // 这是一个异常});
}Future testThen() {return new Future(() {throw ('这是一个异常');});
}

2. catchError

  1. 它比 then/onError 的接受范围要广,它不仅能接受 Future 中的异常,还能接受 then 中的异常;
  2. 如果没有填 then/onError
  3. 如果
void main() {testThen().then((res) {print(res);return "加工后的${res}";}, onError: (err) {print("onError: ${err}");throw ("这是 onError  抛出的异常");}).catchError((err) {print("catchError: ${err}");});
}Future testThen() {return new Future(() {throw ('这是一个异常');});
}
// 执行结果
// onError: 这是一个异常
// catchError: 这是 onError  抛出的异常

3. whenComplete

Future 完成之后总是会调用,不管是错误导致的完成还是正常执行完毕,并且返回一个 Future 对象。类似于 JavaScript 中的 Promise 中的 finally

void main() {testThen().then((res) {print(res);return "加工后的${res}";}, onError: (err) {print("onError: ${err}");throw ("这是 onError  抛出的异常");}).catchError((err) {print("catchError: ${err}");}).whenComplete(() {print("执行完成");});
}Future testThen() {return new Future(() {throw ('这是一个异常');});
}// 执行结果
// onError: 这是一个异常
// catchError: 这是 onError  抛出的异常
// 执行完成

静态方法

1. Future.delayed()

执行一个延时任务。

void main(List args) {print('start....');Future.delayed(Duration(seconds: 3), () => print("延时任务"));print('end....');// 执行结果:// start....// end....// 延时任务
}

注: delayed 中实现的延时操作通过 Timer 来实现的,在实际开发中,如果只是一个单纯的延时操作,建议使用 Timer

void main(List args) {print('start....');new Timer(Duration(seconds: 3), () => print("延时任务"));print('end....');// 执行结果:// start....// end....// 延时任务
}

2. Future.value()

创建一个返回指定 value 值的 Future

void main() {print(1);Future.value("abc").then((res) => print(res));print(2);// 执行结果// 1// 2  // abc
}

不常用,但是同样挺重要的方法

1. Future.wait()

执行多个 Future,等待所有的 Future 执行完成。

void main() {Future.wait([futureA(), futureB()]).then((res) {print(res);});
}Future futureA() {return Future.delayed(Duration(seconds: 3), () {print("futureA 完成了");});
}Future futureB() {return Future.delayed(Duration(seconds: 5), () {print("futureB 完成了");});
}// 执行结果
// futureA 完成了
// futureB 完成了
// [null, null]

2. Future.any()

执行多个 Future,哪个先执行完成,就返回对应的数据。

void main() {Future.any([futureA(), futureB()]).then((res) {// 此时 res 获取的是 futureA() 执行的结果print(res);});
}Future futureA() {return Future.delayed(Duration(seconds: 3), () {print("futureA 完成了");});
}Future futureB() {return Future.delayed(Duration(seconds: 5), () {print("futureB 完成了");});
}
// 执行结果
// futureA 完成了
// null         -----------  Future.any 返回的结果
// futureB 完成了

3. timeout

设置异步操作的超时时长,返回的同样是个 Future.

import 'dart:async';void main() {futureA();
}Future futureA() {return Future(() {new Timer(Duration(seconds: 5), () {print("测试");});}).timeout(Duration(seconds: 3)).then((res) {// 设置超时print(res);print("超时后执行的 then");});
}
// 执行结果
// null
// 超时后执行的 then
// 测试


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部