Flutter Dio 报错is not a subtype of type ‘DioError‘

flutter 通过Dio调用接口报错 “is not a subtype of type 'DioError'”

拦截器中原代码如下:

@override
 onResponse(Response response) async{

  RequestOptions option = response.request;
  try {

    ///一般只需要处理200的情况,300、400、500保留错误信息
    if (response.statusCode == 200 || response.statusCode == 201) {
      int code = response.data["code"];
      if (code == 0) {
        return new ResultData(response.data, true, ExceptionHandle.success,
            headers: response.headers);
      } else {
        return new ResultData(response.statusMessage, false, ExceptionHandle.success,
            headers: response.headers);
      }
    }
  } catch (e) {
    print(e.toString() + option.path);

    return new ResultData(ExceptionHandle.handleException(e).msg, false, response.statusCode,
        headers: response.headers);
  }

  return new ResultData(response.statusMessage, false, response.statusCode,
      headers: response.headers);
}

参考:https://blog.csdn.net/qq_34205937/article/details/108358553

修改后报错

后根据大佬的思路修改为:

@override
   onResponse(Response response) async{
    Map<String,dynamic> responseData = jsonDecode(response.toString());
    try {

      ///一般只需要处理200的情况,300、400、500保留错误信息
      if (response.statusCode == 200 || response.statusCode == 201) {
        String code = responseData["code"];
        if (code=="0000") {
          return new ResultData(responseData["data"], true, ExceptionHandle.success,
              headers: response.headers);
        } else {
          return new ResultData(response.statusMessage, false, ExceptionHandle.success,
              headers: response.headers);
        }
      }
    } catch (e) {

      return new ResultData(ExceptionHandle.handleException(e).msg, false, response.statusCode,
          headers: response.headers);
    }

    return new ResultData(response.statusMessage, false, response.statusCode,
        headers: response.headers);
  }

解决了我所遇到的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值