当两个接收器之一更新时,带有流的Dart BLoC需要更新

时间:2018-12-20 13:21:01

标签: dart flutter rxdart

我想拥有一个BLoC类,其中两个输入(接收器)中的一个或两个都更新时,流输出需要更新。

当任何一个接收器更新/不同时,如何连接Steam触发?

import 'package:escapemodels/src/bloc/blocbase.dart';
import 'package:rxdart/rxdart.dart';

class MathAddBloc implements BlocBase {
  final Sink<int> x;
  final Sink<int> y;
  final Stream<int> addxy;

  factory MathAddBloc() {

    final x = PublishSubject<int>();
    final y = PublishSubject<int>();

  // This is the part that I am confused about - I can figure how to trigger when one Sink/Observable changes but not when either one is updated.
    final addxy = x
    .distinct()
    .switchMap<int>((int x) => (x + y.last))
    .startWith(0);

    return MathAddBloc._(x,y, addxy);
  }


  MathAddBloc._(this.x, this.y, this.addxy);

  void dispose() {
    x.close();
    y.close();
  }
}

1 个答案:

答案 0 :(得分:0)

我认为您需要combineLatest2方法。

您可能也想看看this video,我发现这样做非常有用。