【发布时间】:2020-09-02 11:42:14
【问题描述】:
当您想在 StreamBuilder 中使用多个流时,有什么好方法可以重新启动 StreamBuilder 以使用下一个流?
现在,我正在通过在标志 (_streamCompleted) 上使用 setState 来检查流是否已完成。
但我想知道在 ConnectionState.done 被触发后是否可以重用 StreamBuilder。
Stream donwloadStream = response.stream.map(processDownload);
...
StreamBuilder(
stream: _streamIndex == null ? null : streams[_streamIndex].stream,
builder: (context, snapshot) {
if (ConnectionState.done) {
// How do I switch to the next stream here ???
}
这是我检查是否完成的方式:
double processDownload(List<int> chunk) {
bytes.addAll(chunk);
cnt = bytes.length;
double progress = cnt / streams[_streamIndex].contentLength;
if (cnt / streams[_streamIndex].contentLength == 1) {
setState(() {
_streamCompleted = true;
_streamIndex++;
});
}
return progress;
}
我也尝试过使用 CombineLatestStream,但无法让流开始触发,ConnectionState 为无。
StreamBuilder(
stream: _combinedStreams == null ? null : _combinedStreams,
setState(() {
_combinedStreams =
CombineLatestStream.list([streams[0].stream, streams[1].stream, streams[2].stream])
.asBroadcastStream();
});
_combinedStreams.listen((event) {});
【问题讨论】: