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

Sebastian picture Sebastian · Jul 28, 2018 · Viewed 14.7k times · Source

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?

Answer

shashikant durge picture shashikant durge · Apr 21, 2019

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
)