diff --git a/dio/lib/src/adapter.dart b/dio/lib/src/adapter.dart index 5b8ebd3e5..7d3b589a3 100644 --- a/dio/lib/src/adapter.dart +++ b/dio/lib/src/adapter.dart @@ -75,7 +75,9 @@ class ResponseBody { this.statusMessage, this.isRedirect = false, Map>? headers, - }) : stream = Stream.value(Uint8List.fromList(bytes)), + }) : stream = Stream.value( + bytes is Uint8List ? bytes : Uint8List.fromList(bytes), + ), headers = headers ?? {}; /// Whether this response is a redirect. diff --git a/dio/lib/src/adapters/io_adapter.dart b/dio/lib/src/adapters/io_adapter.dart index e89a927d0..e0683300e 100644 --- a/dio/lib/src/adapters/io_adapter.dart +++ b/dio/lib/src/adapters/io_adapter.dart @@ -200,7 +200,10 @@ class IOHttpClientAdapter implements HttpClientAdapter { } int dataLength = 0; - final stream = responseStream.transform( + + /// Casting the stream to Uint8List is safe due to [_HttpClientResponse] + /// working on a Uint8List. + final stream = responseStream.cast().transform( StreamTransformer.fromHandlers( handleData: (data, sink) { if (cancellation != null) { @@ -226,7 +229,7 @@ class IOHttpClientAdapter implements HttpClientAdapter { dataLength += data.length, responseStream.contentLength, ); - sink.add(Uint8List.fromList(data)); + sink.add(data); }, ), );