Flutter redux epics – takeUntil action make stream unusable

Issue

I am using flutter with redux and for handling stream data I’m using redux-epics.

Like this:

Stream<dynamic> getDataEpic(Stream<dynamic> actions, EpicStore<AppState> store) {
  return Observable(actions)
      .ofType(TypeToken<RequestDataAction>())
      .flatMap((RequestDataAction action) {
        return getDataStream()
          .map(
              (data) => UpdateStateWithData(data)
          );
      })
      .takeUntil(actions.where((action) => action is CancelGetDataAction));
}

// getDataStream() - is a stream that returns some stuff... 

In my screen in onInit I call store.dispatch(RequestDataAction()) and onDispose I call store.dispatch(CancelGetDataAction()) which destroys the Observable altogether, so the next time I go to this screen if I call store.dispatch(RequestDataAction()) my stream is not sending data, actually the entire Observable is dead!

How can I solve this problem? As far as I can see the problem is takeUntil because I completely closes the observable..

Solution

Try moving the takeUntil into the flatMap just after the getStreamData().map(). This will cancel the inner observable rather than the outer observable when the action is received.

Answered By – exilonX

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *