switchMap<R> method

ReactiveSubject<R> switchMap<R>(
  1. ReactiveSubject<R> mapper(
    1. T event
    )
)

Transforms the items emitted by the source ReactiveSubject by applying a function that returns a ReactiveSubject, then emitting the items emitted by the most recently created ReactiveSubject.

Usage:

final subject = ReactiveSubject<int>(initialValue: 1);
final switched = subject.switchMap((i) => ReactiveSubject<String>(initialValue: 'Value: $i'));
switched.stream.listen(print);
subject.add(2); // Prints: Value: 2

Implementation

ReactiveSubject<R> switchMap<R>(ReactiveSubject<R> Function(T event) mapper) {
  final newSubject = ReactiveSubject<R>();
  stream
      .switchMap((event) => mapper(event).stream)
      .listen(newSubject.add, onError: newSubject.addError);
  return newSubject;
}