Flutter网络请求关闭Loading对话框
使用场景
在使用Flutter中,无论是异步任务执行成功与否,都要进行网络请求前的对话框加载,在请求结束后进行对话框的关闭。
面对这种场景,有两种方法,
一种是在then
或catch
中关闭一下对话框,
第二种就是使用Future
的whenComplete
回调
代码如下:
Future.delayed(new Duration(seconds: 2),(){
//return "hi world!";
throw AssertionError("Error");
}).then((data){
//执行成功会走到这里
print(data);
}).catchError((e){
//执行失败会走到这里
print(e);
}).whenComplete((){
//无论成功或失败都会走到这里
});
所以按照经验,我们直接在whenComplete
中进行弹框的关闭就可以。
还没有评论,来说两句吧...