如何阅读从Htt的prequest所有二元,在飞镖? [英] How to read all binary from a HttpRequest, in Dart?
问题描述
的Htt prequest
在飞镖,是一个流<名单< INT>>
,如何获得二进制内容列表< INT>
HttpRequest
in Dart, is a Stream<List<int>>
, how to get the binary content as List<int>
?
我试过:
request.toList().then((data) {
// but data is a List<List<int>> !!!
});
看来我不应该使用了ToList
。什么是正确的方法,我应该使用?
It seems I should not use toList
. What's the correct method I should use?
推荐答案
要做到这一点是设置为的
。当它已经完成了回调被触发。然后,只需创建整数列表,并添加到它每次每个事件: onDone
回调单程听( )
One way to do it is to set the onDone
callback for listen()
. The callback is fired when it has completed. Then just create a list of integers and add to it per every event:
List<int> data = [];
request.listen(data.addAll, onDone: () {
// `data` has here the entire contents.
});
另外,这里是一个班轮:
Alternatively, here's a one-liner:
request.reduce((p, e) => p..addAll(e)).then((data) {
// `data` has here the entire contents.
});
我还将添加安德斯·约翰森的使用 BytesBuilder
类,我想你应该preFER的伟大的提示:
I'll also add Anders Johnsen's great tip of using BytesBuilder
class, which I think you should prefer:
request.fold(new BytesBuilder(), (b, d) => b..add(d)).then((builder) {
var data = builder.takeBytes();
});
这篇关于如何阅读从Htt的prequest所有二元,在飞镖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!