0

我想要一个 BLoC 类,当两个输入(接收器)中的一个/两个都更新时,需要更新 Stream 输出。

当任一 Sink 更新/不同时,如何连接 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();
  }
}
4

1 回答 1

1

我想你想要combineLatest2方法。

你可能也想看看这个视频,我发现它在做类似的事情时非常有用。

于 2018-12-20T14:00:05.967 回答