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 BuildContext
to use Navigator.of()
function to navigate. Let me show you how...
Note: I'm assuming here that you already know BLoC, if not just follow these link: https://felangel.github.io/bloc/
To navigate in reaction to a BLoC's state we sometimes do something like betwen our widgets:
It works but doesn't seems to be the right way because we need to check the state in the UI and wait until Flutter render the last frame to navigate. And then we think, but we can't navigate inside a BLoC because we don't have a BuildContext there!
Navigate without a BuildContext
Basically we use the navigatorState to navigate without a BuildContext, keep reading to see how it works.
- Create your
NavigatorBloc
that receive a global key with the navigator state in the constructor. This bloc should only recieve events to navigate between pages, see the example bellow:
- Create a global key to store with the navigator state and then pass it to your
MaterialApp
. Also we should pass this property to NavigatorBloc.
And we're all set, for each navigation you just create a event to navigate for each specific page. Now we can navigate to a page from any BLoC.
Tip: use BlocProvider.of<NavigatorBloc>(context).dispatch(NavigateToHomeEvent());
inside any widget to navigate to another page using the navigator BLoc. That way you will have a centralized navigation class.
Hope you enjoy the post, don't forget to leave an ❤️ and share the article.
Top comments (21)
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.
Thanks. Now I noticed that I made a typo
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
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...
@override
Stream mapEventToState(NavigatorAction event) async* {
if(event is NavigatorActionPop){
navigatorKey.currentState.pop();
}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?
Alex
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, 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) {
showDialog(
context: navigatorKey.currentContext,
builder: (context) {
return RedDialog(
title: 'title',,
message:
'message',,
onNext: () {
Navigator.of(context).pop();
},
nextLabel: 'nest',,
);
});
}
Everything looks good. Are you sure that
ShowError
event is being fired?Hello, I am trying to use your example, but in navigation_bloc.dart, I am getting NavigationAction is not a type.
Can you show me a piece of your code?
I am using flutter_bloc: 0.21.0
This looks like a blocky way to navigate... indirectly
:) I think you're missing the redux dependency
Nice job on the article
Hi, How is it blocky ?
Thank you
Hey!
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.
@pedromassango - absolutely brilliant.
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 way you can implement navigation and add to Stream any other side-effects (for example to show Snackbar or Dialog) stackoverflow.com/a/59347530/7158449