美文网首页
Dart中处理异步操作

Dart中处理异步操作

作者: 刘铁崧 | 来源:发表于2020-11-16 17:04 被阅读0次

    Dart中的异步操作使用Future、async、await

    Future

    • 将耗时操作包裹到Future函数中,一旦有返回结果,将执行Future对应的then的回调
    • 如果有错误信息,需要在Future毁掉中抛出异常,使用catchError捕获异常
      void initState() {
        // TODO: implement initState
        super.initState();
        print("123123");
        getNetworkData().then((String value){
          print(value);
        }).catchError((error){//异常捕获
          print(error);
        }).whenComplete((){
          print("执行完成");
        });
      }
      Future<String> getNetworkData(){
        return Future(
            (){
              sleep(Duration(seconds: 3));
              // return "Finished";
              throw Exception("发现异常");//抛出异常
            }
        );
      }
    

    链式调用

      void initState() {
        // TODO: implement initState
        super.initState();
        print("开始请求数据");
        getNetworkData().then((String value){
          print("最终:$value");
        }).catchError((error){//异常捕获
          print(error);
        }).whenComplete((){
          print("执行完成");
        });
      }
    
      Future<String> getNetworkData(){
        return Future((){
              sleep(Duration(seconds: 1));
              return "第一次请求数据";
              throw Exception("发现异常");//抛出异常
            }).then((value){
              print("2:$value");
              sleep(Duration(seconds: 1));
              throw Exception("第二次发现异常");
            }).then((value){
              print("3:$value");
              sleep(Duration(seconds: 1));
              return "第三次请求数据";
            });
      }
    
    Performing hot restart...
    Syncing files to device iPhone 12 Pro Max...
    Restarted application in 416ms.
    flutter: 开始请求数据
    flutter: 2:第一次请求数据
    flutter: Exception: 第二次发现异常
    flutter: 执行完成
    

    value()

    Future.value("123").then((value){
          print(value);
    });
    打印:
    flutter: 123
    

    delayed()延迟执行

    Future.delayed(Duration(seconds: 1)).then((value){
          print("延迟1s执行");
    });
    

    await、async的使用

    await和async是dart语言中的关键字,用来实现异步调用
    通常一个async函数会返回一个Future

    使用async进行异步调用:

      void initState() {
        // TODO: implement initState
        super.initState();
        getData().then((value){
          print("异步获取结果:$value");
        });
      }
      // 使用async返回的结果必须为Future
      Future getData() async{
        return "结束后调用";
      }
    

    await + async代替Future的链式调用

    
    
      void initState() {
        // TODO: implement initState
        super.initState();
        testDataGetting().then((value){
          print(value);
        }).catchError((error){
          print(error);
        });
      }
      Future testDataGetting() async {
        var response1 = await getData("测试1");
        var response2 = await getData("测试2:"+response1);
        var response3 = await getData("测试3:"+response2);
        throw Exception("发现异常");
        // return response3;
      }
      // 使用async返回的结果必须为Future
      Future getData(String message) async{
        return Future((){
          return "拼接数据: " + message;
        });
      }
    
    打印:
    flutter: 拼接数据: 测试3:拼接数据: 测试2:拼接数据: 测试1
    

    相关文章

      网友评论

          本文标题:Dart中处理异步操作

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