I'm trying to make a progress bar indicator for a downloading file, but if I add a listener to the StreamedResponse
, the piping works, but does to not finish its future.
final client = new http.Client();
http.StreamedResponse response = await client.send(http.Request("GET", Uri.parse('someurl')));
var received = 0;
var length = response.contentLength;
//if I remove this listener, the await below gets completed
var listen = response.stream.listen((List<int> bytes) {
received += bytes.length;
print("${(received / length) * 100} %");
});
var sink = downloadFile.openWrite();
await response.stream.pipe(sink);
listen.cancel();
sink.close();
On github they already advised someone that it was supposed to work, but on StreamedResponse docs stays that This should always be a single-subscription stream.
. So, adding a listener to calculate the percentage seems to bugs StreamedResponse
pipe in someway. Any idea on how to get this to work?
response.stream,map
for example and update yourreceived
counter there – Diastasis