颤振结合可观察物

时间:2019-03-13 18:07:23

标签: flutter stream observable rxdart combinelatest

我想合并两个可观察变量,并在每个变量都改变时获得两个值。

根据https://pub.dartlang.org/documentation/rxdart/latest/rx/Observable/combineLatest2.html及其大理石图,这正是我想要的。

所以,我有:

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)
            );
        }
    })
]);

问题在于,snapshot.data始终为空。

obsevables(从BehaviorSubject创建以恢复插入到流中的最后一个值)_matchViewModel.lineUpPlayers和_matchViewModel.playerSelectedState在其他StreamBuilder中使用,它们似乎正常工作。

这是怎么了?

1 个答案:

答案 0 :(得分:1)

确保您在两个流中都发布了一个事件,以便能够获取第一个值。