Get Started
This tutorial guides you through installing the package, writing a simple guard, and applying it to both type‑safe and traditional routes.
Prerequisites
Section titled “Prerequisites”- Flutter app using Go Router
- Dart 3.5+, Flutter 3.19+, go_router 16.2.5+
Install
Terminal window flutter pub add go_router_guardsCreate your first guard
class AuthGuard extends RouteGuard {const AuthGuard();@overrideFutureOr<void> onNavigation(NavigationResolver resolver,BuildContext context,GoRouterState state,) async {final isAuthenticated = await authService.isAuthenticated();isAuthenticated ? resolver.next() : resolver.redirect('/login');}}Protect a type‑safe route
@TypedGoRoute<ProfileRoute>(path: '/profile')class ProfileRoute extends GoRouteData with GuardedRoute {const ProfileRoute();@overrideRouteGuard get guard => const AuthGuard();@overrideWidget build(BuildContext context, GoRouterState state) => const ProfileScreen();}Protect a traditional route
final router = GoRouter(routes: [GoRoute(path: '/profile',builder: (context, state) => const ProfileScreen(),redirect: const AuthGuard().toRedirect(),),],);
Next steps
Section titled “Next steps”- Compose multiple guards with
guardAll
,guardAnyOf
, andguardOneOf
. - Apply router‑level protection with
ConditionalGuard(...).toRedirect()
.