简体   繁体   English

Flutter 结合了 observables

[英]Flutter combine observables

I want to combine two observables and get both values when each of then is changed.我想组合两个 observables 并在每个 then 更改时获得两个值。

According to https://pub.dartlang.org/documentation/rxdart/latest/rx/Observable/combineLatest2.html and its marble diagram, it's exactly what I want.根据https://pub.dartlang.org/documentation/rxdart/latest/rx/Observable/combineLatest2.html及其大理石图,这正是我想要的。

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.问题是 snapshot.data 始终为空。

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

What is wrong with this??这有什么问题??

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM