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
.