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);
}
解决了我所遇到的问题