Guide to GoRouter in Flutter
GoRouter is a sophisticated Flutter package that simplifies navigation and deep linking in your Flutter applications. This guide covers everything from setting up GoRouter to handling complex navigation scenarios and error management.
Getting Started with GoRouter
To start using GoRouter, you need to first add the package to your Flutter project and configure it. Begin by creating a GoRouter configuration by defining GoRoute
objects:
import 'package:go_router/go_router.dart';
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => HomeScreen(),
),
],
);
In your app, use this configuration with MaterialApp.router
or CupertinoApp.router
:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
Upgrading to GoRouter
If you’re upgrading from a traditional Navigator-based routing, you can gradually replace your routes with GoRouter. Start by creating a GoRoute for your home screen and progressively add more routes. GoRouter maintains backward compatibility with the Navigator API, but note that destinations navigated using traditional Navigator methods like push
and pop
won't be deep-linkable.
Configuring Routes
When configuring routes, you can specify path parameters and query parameters. Additionally, GoRouter allows for:
- Sub-routes: Configuring child routes within a parent route
- Dynamic Routing Configuration: Modifying routes at runtime using
ValueNotifier
- Nested Navigation: Implementing additional navigators within the app, useful for layouts with persistent elements like a bottom navigation bar
- Initial Location and Debug Logging: Setting a default route and enabling diagnostic logging
Navigation and Linking
Navigating within GoRouter is straightforward. Use context.go()
for changing screens, with URL-based navigation supporting both path and query parameters. GoRouter also allows for imperative navigation using push
and pop
, but these methods may affect browser history management
Redirection
GoRouter enables redirection based on the application state, useful for scenarios like user authentication. Redirection can be specified at both the top-level (on the GoRouter
constructor) and at the route-level (on individual GoRoute
constructors)
Web, Deep Linking, and Transition Animations
For web applications, GoRouter provides excellent support, managing URL-based navigation and deep linking seamlessly. When a deep link is received, GoRouter displays the configured screen based on the URL path
. Additionally, GoRouter allows customizing transition animations for each route, enabling a variety of visual effects during screen transitions
Type-safe Routes and Named Routes
GoRouter supports type-safe routes using the go_router_builder
package, allowing for a more structured and error-resistant routing setup
. Named routes can also be configured, allowing navigation based on route names instead of URLs
Error Handling
Error management in GoRouter is robust, handling various types of errors such as GoError
, AssertionError
, and GoException
. For custom error handling, GoRouter
provides errorBuilder
and errorPageBuilder
callbacks
In summary, GoRouter is a powerful tool for managing navigation in Flutter apps, offering a range of features from simple path-based navigation to complex scenarios involving deep linking and dynamic route configuration. By understanding and utilizing these features effectively, you can significantly enhance the navigation experience in your Flutter applications.