Start a Project

GoRouter In Flutter

Hey Folks 👨🏻‍💻 today we’ll understand the GoRouter In Flutter.

What is the need for GoRouter in Flutter app development

First let’s understand the difference btw the Flutter Navigation 2.0 and GoRouter.

Difference btw GoRouter and Navigator 2.0

GoRouter and Navigator 2.0 are two different routing libraries available in different programming languages: GoRouter is a routing library for Flutter Web and Navigator 2.0 is a routing library for Flutter Mobile.

You can also check out our Flutter app development services for more Flutter app development.

The main difference between GoRouter and Navigator 2.0 lies in their approach to managing and navigating between different pages within a Flutter app. GoRouter is a declarative, widget-based routing library that allows you to define your routes using a set of nested MaterialApp, Scaffold and Navigator widgets. On the other hand, Navigator 2.0 is a new, more powerful version of Flutter’s built-in Navigator class that uses a declarative routing API to manage page transitions.

Another significant difference is that Navigator 2.0 provides support for deep linking, which means you can define routes that can be accessed through a URL, making it easier to share and navigate to specific pages within an app. GoRouter does not provide built-in support for deep linking.

In summary, GoRouter is a routing library for Flutter Web that uses a widget-based approach to manage routes, while Navigator 2.0 is a more powerful version of Flutter’s built-in Navigator class that provides declarative routing and support for deep linking for Flutter Mobile.

Note: If you are a Mobile app developer then you don’t have to use the GoRouter in Mobile application for Navigation. If you don’t want to write boilerplate code for navigation then you can use the GoRouter for mobile application.

Let’s Understand the GoRouter In Flutter and it’s basic usage.

As mobile applications become increasingly complex, routing and navigation have become critical components of app development. Flutter, a popular mobile app development framework, provides the Navigator widget to handle routing and navigation within an app. However, for larger and more complex applications, a more robust routing solution is needed. This is where go_router comes into play.

go_router is a powerful routing library for Flutter that provides an intuitive and declarative way to manage navigation within an app. With go_router, developers can easily define and manage routes, including nested routes and dynamic routes.

Here are some of the features and benefits of using go_router in a Flutter mobile application:

Declarative Routing

go_router uses a declarative approach to routing. Developers define the routes and their associated parameters in a simple and intuitive way. This makes it easy to manage complex routing scenarios, such as nested routes and dynamic routing.

we define three routes: the home route '/', the details route '/details/:id', and the profile route '/profile/:username'. Each route is defined using the GoRoute class, which takes a name and a page builder function as arguments. The page builder function is responsible for building the page that is displayed when the route is navigated to.

To navigate to a route, we use the context.go() method, which takes the name of the route as an argument. We can also include route parameters in the name of the route, such as '/details/1' or '/profile/john'. The GoRouter class automatically extracts the parameters from the route name and passes them to the page builder function as part of the state.params map.

Customizable Navigation

go_router provides customizable navigation options, including push, pop, and replace. Developers can also define custom transition animations and specify transition durations.

we define two routes: one for the home page and another for the settings page. Each route is associated with a handler function that returns the corresponding page widget.

We also define navigation buttons on each page that use the context.go() method to navigate to the other page. This method is provided by the GoRouter library and can be used to navigate to any defined route by specifying its path.

Easy Parameter Passing

go_router makes it easy to pass parameters between routes. Developers can define parameters as part of the route definition and easily access them within the destination route.

we define two routes: the home route '/' and the details route '/details'. The details route takes a query parameter called id which represents the index of the item that we want to display details for. When a user taps on an item in the home page list, we use context.go() to navigate to the details page and pass the item’s index as a query parameter.

In the DetailsPage widget, we retrieve the value of the id query parameter using state.query['id']! and use it to display the details for the selected item.

Here’s a basic example of how to use go_router for navigation in a Flutter app:

Add GoRouter package in pubspec.yaml file

Note: MinDart SDK- 2.18

Now in your Dart code, you can use:

In this example, we define two routes: the home route '/' and the details route '/details'. When the user taps the button on the home page, we use context.go('/details') to navigate to the details page.

Note that we define the routes and their associated page builder functions in the MyApp widget’s constructor. We also define routerDelegate and routeInformationParser properties of the MaterialApp widget to use the go_router package for navigation.

In each page, we build a simple Scaffold widget with an AppBar and a Center widget that contains either a button or text.

That’s it! This basic example shows how easy it is to use GoRouter In Flutter for navigation in a Flutter app.

Conclusion

go_router is a powerful and flexible routing library for Flutter mobile applications. GoRouter In Flutter is definitely worth considering as a routing solution if you’re building complex and large Applications.

We learned about the GoRouter in Flutter in this blog.

Visit the link to get the package of GoRouter in Flutter.

Thanks for reading this blog. You can also check other blogs from here for more knowledge.

Exit mobile version