parseResponse static method

Future<Response> parseResponse({
  1. required StreamedResponse response,
  2. void onProgress(
    1. int transferred,
    2. int total
    )?,
})

Implementation

static Future<http.Response> parseResponse({
  required http.StreamedResponse response,
  void Function(int transferred, int total)? onProgress,
}) async {
  final List<int> data = [];
  final correctedLength = int.tryParse(
        response.headers["data-length"] ?? "",
      ) ??
      0; // Using Frank's cheater header (data-length) to get the proper size

  try {
    await for (final value in response.stream) {
      data.addAll(value);
      // Check if we need to send progress
      if (onProgress != null) {
        onProgress(data.length, correctedLength);
      }
    }
  } on Exception catch (ex) {
    logger.error("Error in comms execute stream", ex: ex);
    return http.Response("Error in comms execute stream", 500);
  }

  return http.Response(
    String.fromCharCodes(data),
    response.statusCode,
    headers: response.headers,
  );
}