onResponse method
The callback will be executed on success.
If you want to continue the response, call handler.next
.
If you want to complete the response with some custom data directly,
you can resolve a Response object with handler.resolve
and other
response interceptor(s) will not be executed.
If you want to complete the response with an error message,
you can reject a DioError object with handler.reject
.
Implementation
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
isShowErrorMsg = true;
var requestUrl = response.requestOptions.uri.toString();
requestUrl = Uri.decodeComponent(requestUrl);
var statusCode = response.statusCode;
if (statusCode != 200) {
/// 非200会在http的onError()中
} else {
if (!requestUrl.contains('hideLog')) {
XLog.json(response.data, '请求url:$requestUrl');
}
if (response.data is Map) {
Map data = response.data;
RespData respData = mapToRespData(data);
if (respData.success) {
// 将 data 外的数据放到 date 内部
if (respData.containKeys != null && data.length > respData.containKeys!.length) {
data.forEach((key, value) {
if (!respData.containKeys!.contains(key)) {
if (respData.data is Map) {
respData.data[key] = value;
}
}
});
}
response.data = respData.data;
return handler.resolve(response);
} else {
if (requestUrl.contains('hideMsg')) {
isShowErrorMsg = false;
}
return handleFailed(respData, handler, response.requestOptions);
}
} else {
XLog.e(response.data, '请求的结果不是jsonMap');
return handler.reject(DioError(
requestOptions: response.requestOptions,
error: '请求的结果不是jsonMap',
));
}
}
}