GoRouter.dev

Current field guide · 2026

Load a resource after matching its URL

Use the URL to identify data, then give loading, missing, error, and ready states explicit UI.

Edition published 21 Oct 2026go_router 18.1.1 reference
On this page

Matching /books/42 proves that the URI fits a route template. It does not prove that book 42 exists, that the network is available, or that the current account may read it.

Keep the request lifecycle stable

Resolve the identifier at the screen boundary. Use your normal state-management or repository layer to load it, and avoid creating a new request on every widget rebuild. When the identifier changes, cancel or disregard an obsolete request so late results do not overwrite a newer screen.

Design four outcomes

Show a loading state, a useful missing-resource state, a recoverable error, and the loaded content. Authentication and authorization failures need deliberate handling rather than a generic empty page.

A route should be able to reconstruct the screen from its URI. Passing a previously loaded object through extra can be an optimization, but it should not be the only way to display a resource after a fresh entry or reload.

dart / example
GoRoute(
  path: '/books/:bookId',
  builder: (_, state) => BookScreen(
    id: state.pathParameters['bookId']!,
  ),
);
// BookScreen owns loading/error/missing/ready states.
// Its repository fetches data using the validated ID.
Earlier edition: read the complete original guide. Its older APIs are kept in historical context.
Additional historical edition: Wayback snapshot, 2022-06-27.

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.

Unknown URL, missing book, or retryable load?

A URL that does not match a route and a resource that cannot be loaded need different recovery actions. This complete example uses a deterministic local repository: its book names and temporary failure are invented teaching data. It does not send a request or collect an account.

  1. Download the project and keep its pubspec.lock. The checked environment is Flutter 3.47.8, Dart 3.13.7 and go_router: 18.1.1. Use your own compatible SDK; this download does not install one. Run flutter pub get, flutter analyze --no-pub, flutter test --no-pub and flutter build web --release --csp --no-web-resources-cdn --no-wasm-dry-run --no-pub.
  2. Inspect createRouter. It matches only / and /books/:id. /not-a-route reaches errorBuilder and displays “Unknown URL”. Home goes to a known route. This UI does not claim that a server returned HTTP 404.
  3. Open /books/missing. The route matches, but the repository returns null. The screen displays “Book not found” and retains the URI. Do not redirect to the home page merely to hide a missing record.
  4. The default /books/retry deliberately fails once. “Retry” creates a new Future for the same identifier and keeps the URI. Its second attempt displays “Book retry”. The Future is stored in State, rather than recreated on each build; a ValueKey replaces the screen when the identifier changes.
  5. Read the four widget tests. They cover unknown route → Home, matched-but-missing, failure → successful retry without navigation, and a successful known identifier. No real network or production database is involved.

Checked here on 11 November 2026: analyze returned no issues, all four widget tests passed, and a release web build completed. The downloadable JSON records commands, exit codes and log hashes. These checks do not certify Android/iOS, an HTTP backend, or a Flutter web session in your browser.

Adaptation worksheet: incoming URI __; route matched? __; repository outcome __; visible state __; recovery action __; URI after recovery __. For /books/retry the filled answer is yes / temporary failure / Could not load / Retry / unchanged. An authorization failure needs your application's deliberate access UI; this sample does not implement login, cancellation or every possible server status.

Complete project, four tests and lockfile · ZIP · Check commands and results · JSON

Unknown URL goes to router error UI; a matched URL loads data, then displays missing, retryable failure or ready content. Retry preserves the URI.
Choose recovery from the failed step. Diagram: GoRouter.dev, CC BY 4.0.

go_router 18.1.1 · Router error handling source

Find your next route