[Dart] Future

作者: PPPaix | 来源:发表于2020-03-17 09:31 被阅读0次

异步编程

Dart中通过 FutureStream 实现异步,谈论他们之前,先简单了解一下什么叫异步。

Asynchrony, in computer programming, refers to the occurrence of events independent of the main program flow and ways to deal with such events. These may be "outside" events such as the arrival of signals, or actions instigated by a program that take place concurrently with program execution, without the program blocking to wait for results.
-- https://en.wikipedia.org/wiki/Asynchrony_(computer_programming)

异步是独立于“主程序流”的一个流,类似于子线程,可以在不阻塞主线程的情况下,执行并返回结果,比如从数据库读取数据,通过异步的形式,可以在读取数据时执行加载动画,等待读取到数据将其显示到界面上。

Future

Dart 中的 Future 类,搭配 await , async 关键字可以实现简单的异步。

下面的代码模拟一个获取name值的耗时操作:

//模拟耗时操作,获取到 name 需要 2 秒。
Future<String> getName() {
 return Future.delayed(Duration(seconds: 2), () => 'Tom');
}
​
//获取到 name 后将其显示出来。
void printName() {
 var name = getName();
 print(name);
}
​
void main() {
 printName();
 print('something else');
}

输出结果为:

可以看到,main()里的两个方法是按代码顺序同步执行的,执行 printName() 方法时,getName()立即返回了一个Future并被打印,然后才打印 “Tom”。

下面改下代码:

//模拟耗时操作,获取到 name 需要 2 秒。
Future<String> getName() {
 return Future.delayed(Duration(seconds: 2), () => 'Tom');
}
​
//获取到 name 后将其显示出来。
void printName() async{
 var name = await getName();
 print(name);
}
​
void main() {
 printName();
 print('something else');
}

输出结果为:


虽然 printName() 的执行需要两秒,但没有阻塞其他代码的执行,"something else "先被打印,两秒后 "Tom" 被打印出来。
改动的部分是 Lines7 的async和 Lines8 的awaitasync表示程序需要异步执行,await表示等待异步代码执行完成。

代码说明:

  • Future.delayed(Duration duration, [FutureOr<T> computation()])
    这个方法可以便捷地生成一个Future对象,延迟 duration 时间执行 computation() ,并返回一个
    T类型的对象。

  • await
    等待 Future 返回执行完成后结果,代码遇到 await 会等待 Future 返回结果,再执行下一句代码。
    返回值是 Future<T>的方法,如果不加 await,是立即返回一个 Future<T> 对象;加上 await,则待 Future执行完毕后返回 T 类型的对象。

  • async
    标记一个方法为异步方法,使用到 await 关键字的方法必须加 async,否则会提示 "The await expression can only be used in an async function."

捕获异常

跟同步代码捕获异常的方式一样,使用try-catch,代码如下:

Future<String> getName() {
  return Future.delayed(
      Duration(seconds: 2), () => throw Exception('Something wrong'));
}

void printName() async {
  try {
    var name = await getName();
    print(name);
  } catch (e) {
    print(e.toString());
  }
}

void main() {
  printName();
  print('Something else');
}

输出结果:


参考链接

相关文章

  • Dart中处理异步操作

    Dart中的异步操作使用Future、async、await Future 将耗时操作包裹到Future函数中,一...

  • [Dart] Future

    异步编程 Dart中通过 Future 和 Stream 实现异步,谈论他们之前,先简单了解一下什么叫异步。 As...

  • dart异步

    Dart中的异步 Dart语言的异步编程之Future和async-await是杀手级功能

  • flutter 中的Future方法读取踩的小坑

    flutter dart 语法中 Future其实和js中的Promse 原理是一样的 Future 声明的函数...

  • dart--future

    What's the point?Dart code runs in a single “thread” of e...

  • Dart Future 使用

    自己要做插件了,昨天看了一下Future ,做一下简单的总结,以便后期使用 这是官方地址Dart Future自...

  • 基于FutureBuilder通用网络请求界面封装

    基于FutureBuilder通用网络请求界面封装 Dart异步编程Future 在Dart中,处理异步有两种方式...

  • Dart异步编程-future

    Dart异步编程包含两部分:Future和Stream该篇文章中介绍Future 异步编程:Futures Dar...

  • Dart异步任务与消息循环机制

    Dart异步任务与消息循环机制 Future , async/await 表示异步任务 一个Dart应用有一个消息...

  • Dart基础7-异步

    java中用thread来支持异步操作,而dart中用的是Future Future 表示在将来某时获取一个值的方...

网友评论

    本文标题:[Dart] Future

    本文链接:https://www.haomeiwen.com/subject/ysjzehtx.html