Home > Enterprise >  Execute a method if stream has the right value in it | Flutter | RxDart
Execute a method if stream has the right value in it | Flutter | RxDart

Time:08-11

Let us say that I have a stream (A) that could either get : SuccessModel or the FailureModel.

  • I subscribe to this stream A using a StreamBuilder and well, I just want to route to the next page if this stream contains SuccessModel and stay at the same page if it contains the FailureModel.
  • Now, since this is a StreamBuilder it always expects me to return a Widget but in this case I might want to just use Navigator to route to the next page.

Considering the above scenario, how should I go about it?

Should I just return an empty container and probably Future.delayed into the next page (upon success) or is there a method that I could use on my stream A so that I define a function or listener inside of my bloc class and it executes the right set of functions (routes) based upon the value that stream gets..

Any help would be appreciated!

CodePudding user response:

So you have a stream and you want your UI to redirect to the right Route, depending on stream's value.

If you're using a Router 1.0, just listen to stream inside your widget and there decide whether to push a route or not.

Or, if you're using a Router 2.0, and maybe with GoRouter you can listen to that stream directly in your ChangeNotifier class, so that you redirect your user based on stream's value.

  • Related