Skip to content

Get Started

This tutorial guides you through installing the package, writing a simple guard, and applying it to both type‑safe and traditional routes.

  • Flutter app using Go Router
  • Dart 3.5+, Flutter 3.19+, go_router 16.2.5+
  1. Install

    Terminal window
    flutter pub add go_router_guards
  2. Create your first guard

    class AuthGuard extends RouteGuard {
    const AuthGuard();
    @override
    FutureOr<void> onNavigation(
    NavigationResolver resolver,
    BuildContext context,
    GoRouterState state,
    ) async {
    final isAuthenticated = await authService.isAuthenticated();
    isAuthenticated ? resolver.next() : resolver.redirect('/login');
    }
    }
  3. Protect a type‑safe route

    @TypedGoRoute<ProfileRoute>(path: '/profile')
    class ProfileRoute extends GoRouteData with GuardedRoute {
    const ProfileRoute();
    @override
    RouteGuard get guard => const AuthGuard();
    @override
    Widget build(BuildContext context, GoRouterState state) => const ProfileScreen();
    }
  4. Protect a traditional route

    final router = GoRouter(
    routes: [
    GoRoute(
    path: '/profile',
    builder: (context, state) => const ProfileScreen(),
    redirect: const AuthGuard().toRedirect(),
    ),
    ],
    );
  • Compose multiple guards with guardAll, guardAnyOf, and guardOneOf.
  • Apply router‑level protection with ConditionalGuard(...).toRedirect().