go_router
Flutter’s built-in Navigator is imperative and has no concept of URL paths or nested routes. For anything beyond a simple push/pop flow — deep links, URL params, nested navigation, web URLs — you reach for the official go_router package. It brings a declarative route configuration that maps almost directly onto React Router v6’s createBrowserRouter.
go_router is maintained by the Flutter team and is the officially recommended routing solution for production Flutter apps.
Note:
go_routerrequires addinggo_routerto yourpubspec.yaml. It cannot be run in DartPad, so code in this lesson is for reference only.
Side-by-side comparison
Section titled “Side-by-side comparison”// React Router v6import { createBrowserRouter, RouterProvider, useParams } from 'react-router-dom';
const router = createBrowserRouter([ { path: '/', element: <RootLayout />, children: [ { index: true, element: <HomeScreen /> }, { path: 'products', element: <ProductList /> }, { path: 'products/:id', element: <ProductDetail /> }, ], },]);
// In ProductDetail:const { id } = useParams();// go_routerimport 'package:go_router/go_router.dart';
final router = GoRouter( routes: [ ShellRoute( builder: (context, state, child) => RootLayout(child: child), routes: [ GoRoute( path: '/', builder: (context, state) => const HomeScreen(), ), GoRoute( path: '/products', builder: (context, state) => const ProductList(), ), GoRoute( path: '/products/:id', builder: (context, state) { final id = state.pathParameters['id']!; return ProductDetail(id: id); }, ), ], ), ],);
// Navigate:context.go('/products/42');