Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter BLoC pattern - How can I navigate to another screen after a stream event?

Tags:

My question is about navigation used with the BLoC pattern.

In my LoginScreen widget I have a button that adds an event into the EventSink of the bloc. The bloc calls the API and authenticates the user. Where in the LoginScreen Widget do I have to listen to the stream, and how do I navigate to another screen after it returns a success status?

like image 296
Sebastian Avatar asked Jul 28 '18 03:07

Sebastian


People also ask

How do I go to previous screen in Flutter?

This is using context and sometimes we find shortcuts to do the task easily. For that, we have Get. back() in flutter. We can send the result to the previous route and do the operations.

Should I use bloc in Flutter?

Bloc is a good pattern that will be suitable for almost all types of apps. It helps improve the code's quality and makes handling states in the app much more manageable. It might be challenging for someone who is just beginning to use Flutter because it uses advanced techniques like Stream and Reactive Programming.


1 Answers

Use BlockListener.

 BlocListener(   bloc: _yourBloc,   listener: (BuildContext context, YourState state) {     if(state is NavigateToSecondScreen){       Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {return SecondScreen();}));     }   },   child: childWidget ) 
like image 131
shashikant durge Avatar answered Sep 18 '22 15:09

shashikant durge