ReactiveX / rxdart

The Reactive Extensions for Dart
http://reactivex.io
Apache License 2.0
3.36k stars 271 forks source link

why isnt there a concatMap operator when there was one in 2020 #670

Closed faisalmushtaq007 closed 2 years ago

faisalmushtaq007 commented 2 years ago

Help needed. RxDart - Using map and concatMap But i checked there isnt any method like that. Actually what my task is that i have multiple apis in an Iterable. I want to execute that sequentially and ones response is parameter to another api. Can you please help me here.

frankpepermans commented 2 years ago

AsyncExpand is what you want, since this is natively supported by Dart, it made no sense to create a concatMap synonym

faisalmushtaq007 commented 2 years ago

@frankpepermans Can you show an example of it, how to use for multiple apis. I've been stuck with this for long. please!

hoc081098 commented 2 years ago

@faisalmushtaq007 hope it helps you

import 'package:rxdart/rxdart.dart';
import 'dart:async';

Stream<String> task(int i, String param) =>
  Rx.fromCallable(() => 'index $i: time=${DateTime.now()} - param=$param\n')
    .delay(const Duration(seconds: 1));

void main() async {
  String acc = null;

  await Stream.fromIterable([1, 2, 3, 4, 5])
    .asyncExpand((i) =>
      task(i, acc)
        .doOnData((v) => acc = v)
    )
    .forEach(print);
}