调用返回。flutter: Instance of 'Future<dynamic>'

来源:4-4 登录态保持与接口鉴权处理

梦94

2021-10-04

Future fire(BaseRequest request) async {
HiNetResponse? response;
var error;
try{
response = await send(request);
} on HiNetError catch(e){
error=e;
response=e.data;
printLog(e.message);
}catch(e){
//其他异常
error = e;
printLog(e);
}

if(response == null){
  printLog("error:$error");
}

var result = response?.data;
printLog("result:$result");
var status = response?.statusCode;
switch(status){
  case 200:
    return result;
  case 401:
    throw NeedLogin();
  case 403:
    throw NeedAuth(result.toString(),data: result);
  default:
    throw HiNetError(status ?? -1, result.toString(),data: result);
}

}

写回答

2回答

CrazyCodeBoy

2022-09-09

因为
HiNet.getInstance().fire
返回的是个future,通过
then的方式可以获取future执行的结果。
0
0

梦94

提问者

2021-10-04

var notice = HiNet.getInstance().fire(NoticeRequest());
notice.then((value) => {
  print("成功: ${value.toString()}")
});

改成这好了,为何?

0
1

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1722 学习 · 870 问题

查看课程