Skip to content

Flutter BLoC: Right way to Handle navigation

Pedro Massango on July 21, 2019

If you have been using Flutter with BLoc maybe you had the necessity to navigate to a new page inside of your BLoC as we know we don't have a Build... [Read Full]
markdown guide

I only got it working after I changed the code to the following:

child: MaterialApp(
navigatorKey: _navigatorKey,
title: 'My App',
home: HomePage(),

Otherwise, the navigatorKey would not contain any state.


Hi, thanks for sharing.

Any idea why ShowDialog when called from the bloc fails (It does not raise an error, There is just no dialog displayed... :()
} else if (event is ShowError) {
context: navigatorKey.currentContext,
builder: (context) {
return RedDialog(
title: 'title',,
onNext: () {
nextLabel: 'nest',,


Everything looks good. Are you sure that ShowError event is being fired?


Hi Pedro, I noticed something weird... if in the mapEventToState method you await for the navigator push methods, all following event push will simply be ignored...
Stream mapEventToState(NavigatorAction event) async* {
if(event is NavigatorActionPop){
}else if(event is NavigateToHomeEvent){
AWAIT navigatorKey.currentState.pushNamed('/home'); // This cause all following events to be ignored...

After some digging the navigatorKey.currentState.pushNamed() call never complete... You can add a then((_)=>print('completed')); this will never be called... We might have an issue somewhere here...

Any idea?



Maybe it is not good idea to await since it will block all incoming events. I've never tried await there before. Let me investigate...


Hi Pedro, I am new working with Flutter and I am trying to implement you idea but I am getting a not stop looping. I am evaluating in the child widget with BlocBuilder the child state and sending the a event to my navigator bloc but my child widget doesn't stop reevaluating


We found a way how to navigate without BuildContext, but we need BuildContext to call BlocProvider.of<>(context)... I think I don't understand something, can you explain?


You always need to use the context do dispatch an action. What you can do it to use a unique instance of the NavigatorKey to navigate.


Hey Pedro, I'm not sure I follow still. Is there another way you could explain this, possibly with an example?


This looks like a blocky way to navigate... indirectly

:) I think you're missing the redux dependency

Nice job on the article


Hello, I am trying to use your example, but in navigation_bloc.dart, I am getting NavigationAction is not a type.



I was just looking for a similar solution. I will just check if it works, but right now I think this is not the best way to create a separate Bloc for Navigation only. Imo, the idea behind BloC is so UI doesn't know what happens after user interaction. If you have a Bloc for navigation then UI still knows what happens. It ends up being just a code extraction way, but the logic is not covered in any way.


This way you can implement navigation and add to Stream any other side-effects (for example to show Snackbar or Dialog)

code of conduct - report abuse