我正在使用 http
库来下载一张图片。
final client = http.Client();
final _response = await client.send(http.Request('GET', Uri.parse("my_url")));
听取方法一:(听取方式)
int downloaded = 0;
_response.stream.listen((value) {
// this gets called 82 times and I receive actual image size
downloaded += value.length;
});
听取方式2:(使用StreamBuilder小部件)
int downloaded = 0;
StreamBuilder<List<int>>(
stream: _response.stream,
builder: (_, snapshot) {
// this gets called 11 times and I receive around 1/10 actual image size
if (snapshot.hasData) downloaded += snapshot.data.length;
return Container();
},
);
问题是为什么当新数据到达时,
StreamBuilder
的build()
方法并没有被频繁调用,这简单地使其作为小部件的目的无效。