I'm downloading files using dart:io
's HttpClient
as follows.
General idea:
void download(String url, String path, ...) async {
var len = await getFileSizeIfExists(path);
// download from the last byte
...
final file = File(path).openWrite();
...
request.close().then((response) async {
await response.listen((data) {
file.add(data);
// show progress bar
}).onDone((_)=> file.close());
});
...
}
Now I need to use this download function in a for-loop.
I've used a forEach
previously but now changed it to Future.forEach
as mentioned here. The issue is serializing the download
function.
...
urls = ['...'];
Future<void> process(url) async {
await download(url, ...);
}
Future.forEach(urls, process);
...
But they all fire at once and they all start downloading. This is extremely bad and I need sequential downloads.
I'm using async / await only when I need to access them and not using them everywhere (I think).
I thought await
was supposed to wait for the future to complete.
Please tell me what I can do to download one after the other and not kill the server.
The full code is here in case what I've provided above isn't enough.
Resuming from the 294627 byte
Resuming from the 278243 byte
Resuming from the 81635 byte
Resuming from the 179939 byte
Resuming from the 196323 byte
Resuming from the 163555 byte
Resuming from the 229091 byte
Resuming from the 147171 byte
Resuming from the 311011 byte
Resuming from the 114403 byte
Resuming from the 130787 byte
Resuming from the 179939 byte
Current progress 175.72 KB/582.04 MB
0% [------------------------------------------------------------------------------------] 31.68 KB/581.87 MB ETA 131.6sCurrent progress 191.72 KB/466.23 MB
0% [-------------------------------------------------------------------------------------] 31.68 KB/466.05 MB ETA 30.1sCurrent progress 143.72 KB/577.72 MB
0% [-------------------------------------------------------------------------------------] 31.68 KB/577.58 MB ETA 93.3sCurrent progress 79.72 KB/488.32 MB
0% [-------------------------------------------------------------------------------------] 31.68 KB/488.24 MB ETA 15.8sCurrent progress 287.72 KB/514.25 MB
0% [-----------------------------------------------------------------------------------] 79.68 KB/581.87 MB ETA 6048.5sCurrent progress 271.72 KB/543.99 MB
0% [------------------------------------------------------------------------------------] 31.68 KB/543.73 MB ETA 193.3sCurrent progress 127.72 KB/514.95 MB
0% [------------------------------------------------------------------------------------] 31.68 KB/514.83 MB ETA 282.9sCurrent progress 223.72 KB/517.85 MB
0% [-------------------------------------------------------------------------------------] 31.68 KB/517.63 MB ETA 83.6sCurrent progress 111.72 KB/425.99 MB
0% [-----------------------------------------------------------------------------------] 79.68 KB/466.05 MB ETA 9191.8sCurrent progress 159.72 KB/474.10 MB
0% [------------------------------------------------------------------------------------] 31.68 KB/473.94 MB ETA 153.2sCurrent progress 175.72 KB/479.53 MB
0% [----------------------------------------------------------------------------------] 47.68 KB/577.58 MB ETA 20414.9sCurrent progress 303.72 KB/561.71 MB
0% [---------------------------------------------------------------------------------] 127.68 KB/577.58 MB ETA 22340.6s^C