About

How to Use GoRouter: Declarative, URL-Based Routing in Flutter

Deep links, redirects, and auth guards

go_router is a declarative routing package officially maintained by the Flutter team.

Define routes with GoRoute objects and register them in a GoRouter instance. Navigate with context.go('/home'), mapping 1:1 with URL paths.

Supports all practical features: auth redirects (redirect to /login when not logged in), ShellRoute (persistent bottom tab bar), nested routes, etc.

Implementation Steps

1

Add go_router package (pubspec.yaml)

2

Create GoRouter instance + define routes with GoRoute

3

Connect router with MaterialApp.router()

4

Implement auth guard with redirect callback

Pros

  • ✓ Perfect Web/deep link support with URL-based routing
  • ✓ Stability guaranteed by Flutter team official maintenance

Cons

  • ✗ Initial setup more complex than basic Navigator
  • ✗ Version updates may incur migration costs

Use Cases

Web + mobile hybrid app (URL-based routing required) Apps with authentication flows
← Flutter Navigation Basics: push/pop and Named Routes How to Call a REST API in Flutter: http/dio and JSON →