onResponse method

void onResponse(
  1. HttpClientResponse response,
  2. HttpClientRequest request, {
  3. dynamic responseBody,
})

Implementation

void onResponse(HttpClientResponse response, HttpClientRequest request,
    {dynamic responseBody}) {
  final DateTime endTime = DateTime.now();
  final networkData = _getRequestData(request.hashCode);
  final responseHeaders = <String, dynamic>{};
  final requestHeaders = <String, dynamic>{};

  if (networkData == null) {
    return;
  }

  response.headers.forEach((String header, dynamic value) {
    responseHeaders[header] = value[0];
  });
  request.headers.forEach((String header, dynamic value) {
    requestHeaders[header] = value[0];
  });

  final int requestBodySize =
      json.decode(json.encode(networkData.requestBody)).length;

  networkLogger.networkLog(networkData.copyWith(
    status: response.statusCode,
    duration: endTime.difference(networkData.startTime).inMicroseconds,
    responseContentType: response.headers.contentType?.value,
    requestContentType: request.headers.contentType?.value,
    responseHeaders: responseHeaders,
    responseBody: responseBody,
    errorCode: 0,
    errorDomain: response.statusCode != 0 ? '' : 'ClientError',
    responseBodySize: int.parse(responseHeaders['content-length'] ?? '0'),
    requestBodySize: requestBodySize,
    requestHeaders: requestHeaders,
  ));
}