go_router: Modern Navigation and Deep Links

Google Flutter 13 min min read Updated: Nov 16, 2026 Intermediate
go_router: Modern Navigation and Deep Links
Intermediate Topic 3 of 4

Why go_router?

go_router is the routing package maintained by the Flutter team. It treats every screen as a URL like /product/42. This gives you deep links, working browser back and forward buttons on the web, redirects and nested navigation.

Install

bash flutter pub add go_router

Set It Up

dart import 'package:go_router/go_router.dart'; final GoRouter router = GoRouter( initialLocation: '/', routes: [ GoRoute( path: '/', builder: (context, state) => const HomePage(), routes: [ GoRoute( path: 'product/:id', // /product/42 builder: (context, state) { final id = state.pathParameters['id']!; return ProductPage(id: id); }, ), ], ), GoRoute( path: '/settings', builder: (context, state) => const SettingsPage(), ), ], ); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp.router( routerConfig: router, title: 'My App', ); } }

Note MaterialApp.router instead of the plain MaterialApp.

Navigate

dart context.go('/settings'); // replace the current location context.push('/product/42'); // add on top, so back works like a stack context.pop(); // go back // Query parameters: /search?q=shoes context.go('/search?q=shoes'); // read: state.uri.queryParameters['q']
MethodBehavior
context.go(path)Navigate to a location. Replaces the stack with the matching routes
context.push(path)Push on top of the current stack
context.pop()Go back
context.replace(path)Replace the current route

Named Routes

dart GoRoute( name: 'product', path: '/product/:id', builder: (context, state) => ProductPage(id: state.pathParameters['id']!), ) context.goNamed('product', pathParameters: {'id': '42'});

Redirect: Protect Screens

dart bool isLoggedIn = false; // in a real app this comes from your state final router = GoRouter( redirect: (context, state) { final goingToLogin = state.matchedLocation == '/login'; if (!isLoggedIn && !goingToLogin) return '/login'; if (isLoggedIn && goingToLogin) return '/'; return null; // no redirect }, refreshListenable: authNotifier, // re-check when login state changes routes: [ /* ... */ ], );
Tip: refreshListenable takes any Listenable, such as a ChangeNotifier that tells the router when the user logs in or out.

Extra Data

dart context.push('/details', extra: product); // Read it final product = state.extra as Product;

extra cannot survive a browser reload on the web, so prefer ids in the path and load the data again.

Error Page

dart GoRouter( errorBuilder: (context, state) => Scaffold( body: Center(child: Text('Page not found: ${state.uri}')), ), routes: [ /* ... */ ], )
Check the docs: go_router evolves, and some APIs get renamed. Look at the package page on pub.dev for the version you install.

Conclusion

go_router gives real URLs to your screens. Define routes once, use context.go and context.push, and add a redirect for login.

Quick Summary: Use MaterialApp.router with GoRouter. Path parameters carry ids, redirect guards screens and context.go navigates by URL.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators