美文网首页
flutter try catch和使用loading时的注意事

flutter try catch和使用loading时的注意事

作者: 天渺工作室 | 来源:发表于2020-03-08 12:02 被阅读0次
官方维护的一些 loading 插件(官方的有些插件某些有坑 也可以自己封装)

https://pub.flutter-io.cn/packages/flutter_spinkit

https://www.ctolib.com/huangjianke-flutter_easyloading.html

自己在封装好laoding 插件之后 在http请求的使用过程中 需要注意laoding的隐藏 尽量使用 try catch finally 的方法 如果接口报错 逻辑也会走到finally 中 保证laoding 的隐藏

//showLoadingToast();   hideLoadingToast(); 为封装好的laoding 功能  

demo(e) async{
    try{
      //显示
      showLoadingToast(context);
      var res = await http(context,
          id:e["id"]);
      if (res['code'] == HttpRespCode.ok) {

      } else {
        Utils.toast(context, res['msg']);

      }
    }catch(e){
    //try只要出现错误 就会走到catch回调
    }finally{
      //不管业务走到try 还是catch finally永远都是最后一个执行
      //隐藏  
      hideLoadingToast(context);
    }

  }
image.gif

相关文章

网友评论

      本文标题:flutter try catch和使用loading时的注意事

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