Flutter进阶篇(4)-- Flutter的Future异步详解

Flutter中的异步其实就是用的Dart里面的Future,then函数,回调catchError这些东西。下面举例详细解答一下使用过程和遇到的一些问题,让大家更好的明白异步流程。

声明:本文是作者AWeiLoveAndroid原创,版权归作者AWeiLoveAndroid所有,侵权必究。如若转发,请注明作者和来源地址!未经授权,严禁私自转载!


说明:本文中的所有函数的引用在main函数中:

main() async {
  testFuture();
  testFuture2();
  testFutureCreate1();
  testFutureCreate2();
  testFutureCreate3();
  testThen1();
  testThen2();
  testThen3();
  testThen4();
  testAll();
}

一、认识Future

1.创建Future

void testFuture(){
  Future future = new Future(() => null);
  future.then((_){
    print("then");
  }).then((){
    print("whenComplete");
  }).catchError((_){
    print("catchError");
  });
} 

这里的执行结果是:

then
whenComplete

Futue直接new就可以了。我这里没有具体的返回数据,所以就用匿名函数代替了, Future future = new Future(() => null); 相当于 Future<Null&gt

猜你喜欢

转载自blog.csdn.net/lzw2497727771/article/details/103576887