Flutter combine observables

I want to combine two observables and get both values when each of then is changed.

According to https://pub.dartlang.org/documentation/rxdart/latest/rx/Observable/combineLatest2.html and its marble diagram, it's exactly what I want.

So, I have:

var observable = CombineLatestStream.combine2(_matchViewModel.lineUpPlayers, _matchViewModel.playerSelectedState, (lineUpPlayers, playerSelectedState) {
    return [lineUpPlayers, playerSelectedState];
});

return Stack(children: <Widget>[
    Align(
        alignment: Alignment.topRight,
        child: Padding(
            padding: EdgeInsets.all(8.0),
            child: _buildFormationSelector(match)
        )),
    StreamBuilder(stream: observable, builder: (context, snapshot) {
        if(snapshot.data == null) {
            return new CircularProgressIndicator();
        } else {
            Map<String, Player> lineUpPlayers = snapshot.data[0];
            bool playerSelectedState = snapshot.data[1];
            return Column(
                mainAxisAlignment: MainAxisAlignment
                    .spaceEvenly,
                children: _buildFormation(match, lineUpPlayers)
            );
        }
    })
]);

The problem is that snapshot.data is always null.

The obsevables(created from BehaviorSubject to recover last value inserted in the stream) _matchViewModel.lineUpPlayers and _matchViewModel.playerSelectedState are being used in other StreamBuilders and they seem to work correctly.

What is wrong with this??

1 answer

  • answered 2019-03-14 08:16 jbernach

    Make sure you are posting an event in both streams to be able to get the first value.