【问题标题】:Flutter combine observablesFlutter 结合 observables
【发布时间】:2019-08-04 12:37:43
【问题描述】:

我想组合两个 observables 并在每个 then 更改时获取两个值。

根据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 中使用,它们似乎工作正常。

这有什么问题??

【问题讨论】:

    标签: flutter stream observable rxdart combinelatest


    【解决方案1】:

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

    【讨论】:

    • 正如你所说,我没有播种 _matchViewModel.playerSelectedState,所以这就是 CombineLatestStream.combine2 不是流数据的原因。
    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 2018-11-12
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多