我正在尝试为下载文件制作进度条指示器,但如果我向 中添加一个侦听器StreamedResponse
,则管道可以工作,但不会完成它的未来。
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();
在github 上,他们已经建议某人它应该可以工作,但在StreamedResponse 文档上仍然如此This should always be a single-subscription stream.
。因此,添加一个侦听器来计算百分比似乎以某种方式StreamedResponse
出错。关于如何让它发挥作用的任何想法?