在ConnectionState.done之后使用多个流重新启动StreamBuilder [英] Restart StreamBuilder with multiple streams after ConnectionState.done

查看:92
本文介绍了在ConnectionState.done之后使用多个流重新启动StreamBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您要在StreamBuilder中使用多个流时,重新启动StreamBuilder以使用下一个流时,有什么好方法?

What's a good way when you want to use multiple streams in a StreamBuilder, to restart StreamBuilder to use the next stream?

现在,我正在通过在标志(_streamCompleted)上使用setState检查流是否已完成。

Right now, I am checking if the stream is completed by using setState on a flag (_streamCompleted).

但是我想知道在ConnectionState.done被触发后是否可以重用StreamBuilder。

But I was wondering if it's possible to reuse the StreamBuilder after ConnectionState.done is fired.

 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 ???
      }

这是我检查是否完成的方式:

This is how I am checking if completed:

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为空。

I have also tried using CombineLatestStream, but couldn't get the streams to start firing, ConnectionState is none.

StreamBuilder(
            stream: _combinedStreams == null ? null : _combinedStreams,

    setState(() {
      _combinedStreams =
          CombineLatestStream.list([streams[0].stream, streams[1].stream, streams[2].stream])
              .asBroadcastStream();
    });

  _combinedStreams.listen((event) {});


推荐答案

只需使用两个流生成器。

Just use two stream builder.

StreamBuilder(
    stream: _streamIndex == null ? null : streams[_streamIndex].stream,
    builder: (context, snapshot) {
      if (ConnectionState.done) {
        StreamBuilder(
            stream: _stream2 == null ? null : streams2.stream,
            builder: (context, snapshot2) {...

这篇关于在ConnectionState.done之后使用多个流重新启动StreamBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆