当您要在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为none。
StreamBuilder(
stream: _combinedStreams == null ? null : _combinedStreams,
setState(() {
_combinedStreams =
CombineLatestStream.list([streams[0].stream, streams[1].stream, streams[2].stream])
.asBroadcastStream();
});
_combinedStreams.listen((event) {});
答案 0 :(得分:1)
只需使用两个流生成器。
StreamBuilder(
stream: _streamIndex == null ? null : streams[_streamIndex].stream,
builder: (context, snapshot) {
if (ConnectionState.done) {
StreamBuilder(
stream: _stream2 == null ? null : streams2.stream,
builder: (context, snapshot2) {...