Greetings, fellow Flutter developers! Today, we delve into a package that's been garnering attention in the Flutter community - GoRouter. As an experienced developer, you might already understand the pivotal role robust routing plays in delivering excellent user experience.
And that's why GoRouter deserves a spotlight. It allows you to navigate users through different screens with its URL-based navigation, consequently offering smooth transitions in complex, navigation-related scenarios. So, let's go on a journey to explore the integrals of GoRouter in Flutter!
Be it multiple screens, deep linking, or supporting Cupertino apps for backward compatibility, GoRouter can handle it all. Its usage starts from adding the GoRouter dependency in the pubspec.yaml file of your Flutter project by running the following command:
1 flutter pub add gorouter 2
Invoking GoRouter in the Flutter application involves calling the GoRouter constructor which accepts a routes and the optional initialLocation. The routes parameter defines the URL patterns and launch points for all the routes in the app, while initialLocation serves as the entry point for the app.
Next, we'll look into the main features this declarative routing package offers, such as sub routes, and query parameters, and greatly simplifies your navigation stack.
The GoRouter package brings on board an impressive array of new features, with the following worthy of special mention:
But how does GoRouter compare against traditional routing in Flutter? Let's explore it.
GoRouter brings to the table some key improvements over traditional routing. The use of URL-based API makes navigation straightforward and intuitive and mitigates the need for pushing routes onto the navigation stack.
Also noteworthy is its competence with parsing paths and deep linking, enhancing the application's responsiveness and versatility each time a user navigates to a page. The feature sets GoRouter a class apart, and as a Flutter developer, it's worth considering this efficient package in your upcoming projects.
Let us venture further into GoRouter's capabilities by looking into GoRoute and how it complements GoRouter in a Flutter application.
GoRoute acts as the fundamental building block for GoRouter. It allows us to define URL patterns and map each route to a specific Flutter page.
Take the example of a simple login page or profile page. Using GoRoute, you can map the URL /login or /profile to the corresponding pages. Here’s a small code snippet illustrating this:
1 final _router = GoRouter( 2 routes: [ 3 GoRoute( 4 path: '/login', 5 builder: (context, state) => LoginPage(), 6 ), 7 GoRoute( 8 path: '/profile', 9 builder: (context, state) => ProfilePage(), 10 ), 11 ], 12 ); 13
Here, the builder constructor accepts a BuildContext context and GoRouterState state. The builder then returns the widget to push onto the navigation stack.
One of the really cool things about GoRouter is its redirection support. It serves a vital role in preserving the integrity and user-friendliness of your app. This effectively handles scenarios such as redirecting users from an error screen to a home page or globally redirecting users from a deprecated URL to a new one.
Redirection works by checking each route’s redirect method. If a visitor tries to access a URL matching the route and the redirect returns a non-null value, GoRouter will use that as the new URL. If the redirect returns null, it continues with the matched route.
Check out this example where if a user tries to access /old, they'll be redirected to /new, courtesy of GoRouter.
1 final _router = GoRouter( 2 routes: [ 3 GoRoute( 4 path: '/old', 5 redirect: (state) => '/new', 6 ), 7 GoRoute( 8 path: '/new', 9 pageBuilder: (context, state) => const Text('New Page') 10 ), 11 ], 12 ); 13
This example accentuates the convenience of implementing redirections in your Flutter projects, making the navigation journey snappier. Moving forward, let's delve into the Observer facet of GoRouter.
Talking of GoRouter and not mentioning its Observer is like overlooking one of its core capabilities. This feature is helpful in several navigation-related scenarios, where you want to observe and react to changes in the navigation stack.
Typically, one might use this to track visited pages, send analytic events, or sync URLs to the application **state. You define a GoRouter.observer function that gets called with a GoRouterState each time a navigation event occurs.
Take a look at the below code snippet to elucidate how GoRouter observers work:
1 final _router = GoRouter( 2 routes: [ /* route definitions go here */ ], 3 observers: [ 4 (BuildContext _context, GoRouterState _state) { 5 // Any custom code here to observe route changes. 6 }, 7 ], 8 ); 9
Setting up an observer like this can greatly enhance your control over navigation events in your app and ensure it behaves as expected.
And here we are, at the end of our exploration of GoRouter in Flutter. GoRouter gives us effective solutions to some of Flutter's key routing glitches. Its stand-out features like URL-based navigation, robust handling of deep linking, and enhanced redirection support all serve to enrich the user experience.
GoRouter is definitely shaping up to be a robust solution for Flutter developers all around the world, enabling them to create intuitive mobile apps with smooth navigation. So if you are yet to embrace the GoRouter, consider giving it a try in your next Flutter project. In fact, you can view the complete source code of GoRouter on GitHub to better understand it.
Here's to building more seamless, easy-to-navigate apps with GoRouter! Happy coding, Flutter developers.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.