Intermediate
Navigation Management with go_router in Flutter
· 8 min read
Why go_router?
Compared to Navigator 1.0's push/pop or Navigator 2.0's raw Page/Router APIs, go_router gives you a URL-based, declarative routing layer. Every screen is defined by a path, so on Flutter Web the address bar actually reflects the app's state, and deep linking — jumping straight to a screen from a notification or the browser — comes for free.
Setup and Basic Route Definitions
A GoRouter instance is created with a routes list; each GoRoute takes a path and a builder that says which widget to show for it. You use MaterialApp.router instead of MaterialApp and pass this router as routerConfig.
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => const HomePage(),
),
GoRoute(
path: '/login',
name: 'login',
builder: (context, state) => const LoginPage(),
),
],
);
MaterialApp.router(routerConfig: router)Path Parameters
Prefixing a path segment with : defines a dynamic parameter. That value — a product ID, say — is read through state.pathParameters and passed straight into the builder.
GoRoute(
path: '/urun/:id',
name: 'urun-detay',
builder: (context, state) {
final id = state.pathParameters['id']!;
return UrunDetayPage(urunId: id);
},
)
// Navigation
context.go('/urun/42');
context.goNamed('urun-detay', pathParameters: {'id': '42'});context.go vs context.push
go replaces the current location with the new path; pressing back doesn't return to it, which fits switching between tabs or following a redirect. push adds a new page to the stack, and back returns to the previous one — use push for 'go forward, then be able to come back' flows like tapping a list item to open its detail page.
// go: replaces the current location, back won't return to it
context.go('/urun/42');
// push: adds a new page to the stack, back returns to the previous one
context.push('/urun/42');A Shared Navigation Bar with ShellRoute
Keeping a BottomNavigationBar fixed across tab switches isn't done by redeclaring it in every page's own Scaffold — it's done with ShellRoute. ShellRoute receives the child route's widget as its child parameter and places it inside the shared shell.
ShellRoute(
builder: (context, state, child) {
return Scaffold(
body: child,
bottomNavigationBar: NavigationBar(
selectedIndex: _indexFromLocation(state.uri.toString()),
onDestinationSelected: (index) => context.go(_locationFromIndex(index)),
destinations: const [
NavigationDestination(icon: Icon(Icons.home), label: 'Ana Sayfa'),
NavigationDestination(icon: Icon(Icons.search), label: 'Ara'),
],
),
);
},
routes: [
GoRoute(path: '/', builder: (context, state) => const HomeTab()),
GoRoute(path: '/ara', builder: (context, state) => const SearchTab()),
],
)An Auth Guard with redirect
GoRouter's redirect callback runs on every navigation attempt: return a String path and the user is sent there, return null and the navigation proceeds as requested. This is the natural place to send a logged-out user to /login whenever they try to reach a protected screen.
final router = GoRouter(
redirect: (context, state) {
final isLoggedIn = AuthController.instance.isLoggedIn;
final goingToLogin = state.matchedLocation == '/login';
if (!isLoggedIn && !goingToLogin) return '/login';
if (isLoggedIn && goingToLogin) return '/';
return null; // no redirect
},
routes: [ /* ... */ ],
);Things to Watch Out For
1. Infinite Loops Inside redirect
If redirect ends up guarding the login page itself, the user gets sent to /login, which redirects again, forever. Always check whether the target path is already the login route — something like the goingToLogin check above — before redirecting.
2. Use Named Routes to Survive Path Changes
Hardcoding path strings like context.go('/urun/42') everywhere means that when a path changes (say /urun/:id becomes /katalog/urun/:id), you have to find and replace it across the whole project. Using context.goNamed('urun-detay', pathParameters: {...}) ties every call site to a single source of truth — the route definition is the only place that needs to change.
Conclusion
go_router gives you a consistent navigation model on both mobile and web through URL-based, declarative routes. Build dynamic pages with path parameters, use go versus push in the right place, lean on ShellRoute for shared navigation bars, and use redirect for authentication guarding.