Flutter : stream two Streams into a single screen?

后端 未结 2 1351
误落风尘
误落风尘 2021-02-07 00:41

I have two streams fetching from two different api.

Stream get monthOutStream => monthOutController.stream;
Stream get resultOu         


        
相关标签:
2条回答
  • 2021-02-07 01:13

    You can nest StreamBuilder if needed. Nothing prevents you from doing the following:

    StreamBuilder(
      stream: stream1,
      builder: (context, snapshot1) {
        return StreamBuilder(
          stream: stream2,
          builder: (context, snapshot2) {
            // do some stuff with both streams here
          },
        );
      },
    )
    

    Another solution if this makes sense for you is: Streams are designed to be mergeable/transformed. You could make a third stream that is a merge of the two later streams.

    Ideally for complex stream operations you'll want to use rxdart as it provides a few useful transformer.

    Using rxdart, the fusion of two Observable (which are subclass of Stream) would be the following:

    Observable<bool> stream1;
    Observable<String> stream2;
    
    final fusion = stream1.withLatestFrom(stream2, (foo, bar) {
      return MyClass(foo: foo, bar: bar);
    });
    
    0 讨论(0)
  • 2021-02-07 01:22
    Observable.combineLatest2(
            aStream,
            bStream,
            (a, b, c) =>
            a != '' && b != '');
    

    combineLatestN returns a combined stream

    0 讨论(0)
提交回复
热议问题