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

官方维护的一些 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);
    }

  }
 

猜你喜欢

转载自www.cnblogs.com/tianmiaogongzuoshi/p/12441817.html