6

我正在尝试为下载文件制作进度条指示器,但如果我向 中添加一个侦听器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出错。关于如何让它发挥作用的任何想法?

4

1 回答 1

8

@pskink 评论让我了解适用于sink您正在使用的每种类型的解决方案。

  var length = response.contentLength;
  var received = 0;
  var sink = downloadFile.openWrite();

  await response.stream.map((s) {
    received += s.length;
    print("${(received / length) * 100} %");
    return s;
  }).pipe(sink);

如果要写入文件,另一种方法是观察文件长度

  var length = response.contentLength;
  var sink = downloadFile.openWrite();

  Future.doWhile(() async {
    var received = await downloadFile.length();

    print("${(received / length) * 100} %");
    return received != length;
  });

  await response.stream.pipe(sink);
于 2019-06-18T12:22:52.017 回答