GoRouter.dev

Current field guide · 2026

Redirect without losing the destination

Return a replacement location when a routing condition requires it; return null when navigation may proceed.

Edition published 21 Oct 2026go_router 18.1.1 reference
On this page

Keep the decision small

A redirect chooses a destination. It should not serve as your entire authentication implementation. Let a dedicated session service own sign-in state and let the router observe changes through a suitable Listenable or dependency mechanism.

Preserve a safe return path

If a signed-out user opens a protected location, remember the intended local destination before sending them to sign-in. Validate any return parameter against allowed local routes. Blindly trusting an external return URL creates an open-redirect problem.

Prevent cycles

Do not redirect the sign-in route back to itself. Distinguish loading, signed-out, and signed-in states. The router has a redirect limit, but hitting that limit is a diagnostic symptom, not a working authentication flow.

Authorize on the server too

A client-side redirect controls navigation and presentation. It does not protect private API responses. Test expired sessions, a direct protected link, a sign-out from a nested screen, and Back after sign-in.

dart / example
redirect: (context, state) {
  final signingIn = state.uri.path == '/sign-in';
  if (!session.isSignedIn && !signingIn) {
    return '/sign-in';
  }
  if (session.isSignedIn && signingIn) {
    return '/';
  }
  return null;
},
// session is your application-owned session service.
Earlier edition: read the complete original guide. Its older APIs are kept in historical context.

Restore a validated book destination after demo sign-in

The short redirect above intentionally returns home. A complete return-path example needs two separate rules: remember the intended location before sign-in, then validate and rebuild it before returning. The downloadable example pins go_router 18.1.1, Flutter 3.47.8 and Dart 3.13.7; it is not labelled the latest release.

  1. A signed-out visit to /books/42?tab=notes redirects to sign-in with an encoded local from parameter. Public home remains public.
  2. The demonstration session notifies the router through refreshListenable. Its button changes in-memory demo state; it does not authenticate an account or collect credentials.
  3. After demo sign-in, safeReturn allows only a numeric book path and one optional tab equal to overview or notes. It rejects schemes, authorities, fragments, duplicate tabs and other query keys, then constructs a new URI.
  4. An invalid return goes home. Signing out from the book hides that page and returns to sign-in. Real private data still require server-side authorization.

Checked journey: signed out → book 42/notes → demo sign-in → book 42/notes. An external return and ambiguous parameters fall back to home. Five automated tests cover valid returns, invalid values, the direct-link journey and sign-out, unsafe return fallback, and public home without a redirect loop. Analysis and a release Web build were also executed. Browser inspection of this guide is separate from those Flutter checks.

Complete example, lockfile, tests and run instructions (ZIP). The archive contains source, not a working login backend. Run the listed commands with the pinned SDK; do not infer production authentication security or a browser Back guarantee from a passing demo test.

Restore a validated book destination after demo sign-in
Book 42 + tab=notes · Demo sign-in + validation · Same safe destination.

Source: pub.dev.

Sources & version context

GoRouter.dev documentation edition. Examples illustrate the APIs described in the linked primary documentation. Adapt application-owned widgets and services to your project. They are not an executed Flutter session in this browser.

Find your next route