onResponse method

  1. @override
void onResponse(
  1. Response response,
  2. ResponseInterceptorHandler handler
)

Called when the response is about to be resolved.

Implementation

@override
void onResponse(Response response, ResponseInterceptorHandler handler) async {
  try {
    debugPrint(
        "<-- ${response.statusCode} ${response.requestOptions.baseUrl + response.requestOptions.path}");

    debugPrint("Headers:");
    response.headers.forEach((k, v) => debugPrint('$k: $v'));

    if (response.data == null) {
      debugPrint("Response data: ${response.data}");
    } else if(ResponseType.stream == response.requestOptions.responseType){
      ResponseBody responseBody = response.data;

      debugPrint("Response stream data: ${responseBody.stream}");
    } else if (ResponseType.bytes != response.requestOptions.responseType) {
      debugPrint("Response not bytes data: ${jsonEncode(response.data)}");
    } else {
      // bytes转String并保存
      Uint8List bytes = Uint8List.fromList(response.data!);

      debugPrint("Response bytes data: ${utf8.decode(bytes)}");
    }
  } catch (e) {
    debugPrint("Response bytes error: $e");
  }
  debugPrint("END HTTP -->\n");
  super.onResponse(response, handler);
}