Start a Project

Navigation without Passing Context in Flutter

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.

#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.

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.

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.

#Conclusion

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.😇

Exit mobile version