getStream method

Future<Stream<List<int>>> getStream(
  1. PathUri path, {
  2. void onProgress(
    1. double progress
    )?,
})

Retrieves the content of the file at path.

onProgress can be used to watch the download progress. Possible values range from 0.0 to 1.0.

See:

Implementation

Future<Stream<List<int>>> getStream(
  PathUri path, {
  void Function(double progress)? onProgress,
}) async {
  final request = get_Request(path);
  final streamedResponse = await httpClient.send(request);
  if (streamedResponse.statusCode != 200) {
    final response = await http.Response.fromStream(streamedResponse);
    throw DynamiteStatusCodeException(response);
  }

  final controller = StreamController<List<int>>();
  final contentLength = streamedResponse.contentLength;
  var downloaded = 0;

  streamedResponse.stream.listen(
    (chunk) async {
      controller.add(chunk);
      downloaded += chunk.length;
      if (contentLength != null) {
        onProgress?.call(downloaded / contentLength);
      }
    },
    onDone: () {
      onProgress?.call(1);
      controller.close();
    },
  );

  return controller.stream;
}