2

I need to download a huge file in dart and also show progress while downloading it. So I implemented this function:

import 'dart:io';
Future<void> download(String url, String path) async {
  HttpClient client = HttpClient();
  final request = await client.getUrl(Uri.parse(url));
  request.close().then((HttpClientResponse response) async {
      final file = File(path).openWrite(mode: FileMode.writeOnlyAppend);
      await response.listen((data) {
         file.add(data);
         // show progress bar
         ...
       }, onError: (e) {
         throw e;
       }, onDone: () {
         file.close().then((e) {
           print("Done");
           return;
       });
    });
  });
}

This works great with for downloading 1 file at a time. But I want to able to call it in a loop and control the number of downloads that can occur at once.

Which brings me to this question on how to implement this using a Stream and Completer.

Phani Rithvij
  • 4,030
  • 3
  • 25
  • 60
  • The proposed solution [here](https://stackoverflow.com/a/59533261/8608146) works because it downloads the whole content and then saves it to a file. But that solution is not applicable here as the requirements `huge files` and `show progress` forces me to call `response.listen`, even `response.pipe` doesn't work. And `response.listen` is a stream subscription that I need to serialize some how. – Phani Rithvij Dec 30 '19 at 16:46

0 Answers0