2

我想组合两个 observables 并在每个值发生变化时获取这两个值。

根据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 始终为空。

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

这有什么问题??

4

1 回答 1

2

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

于 2019-03-14T08:16:51.230 回答