Updated 30 November 2023
Hello all!! In this blog, we will learn how can we do the Navigation without passing context in Flutter.
As we all know we need to pass context while we are navigating in our application from one screen to another.
But sometimes we have some context issues. So without getting frustrated from that context issue we can make a global key and use it for navigation.
Check our Flutter app development services page.
So without wasting time, let’s begin the code implementation.
For the implementation of navigation without passing context, firstly we will create a global navigation key in our main dart file as shown in below code snippet.
1 |
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); |
After creating the global navigation key, we only need to use this key where we want to navigate on another screen. Also if we are getting some context issues while navigating we can use this key.
As it is a global key we can call it in any of the files.
As shown in below code snippet we are using a global navigation key for navigation.
1 |
navigatorKey.currentState?.pushNamedAndRemoveUntil(Login,(route) => false); |
Here we are using the navigatorKey as context and passing it in the place of context.
Not only in navigation, we can also use this key in all the context-required places as it is a global key.
Now we are done with our implementation of Navigation in our app without passing context. It will be very beneficial when we are facing some issues related to context and are unable to navigate.
If we are having some issues while navigating, we can define the global navigation key and use it wherever we want to navigate.
It is also more useful as we are not using any package or dependency in our implementation.
Congratulations!!!! We have implemented Navigation without passing context in flutter.
For more interesting blogs check out here – https://mobikul.com/blog/
Thanks for reading.😇
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.