Skip to content

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_router requires adding go_router to your pubspec.yaml. It cannot be run in DartPad, so code in this lesson is for reference only.

React
// React Router v6
import { 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();
Flutter
// go_router
import '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');
What is the go_router equivalent of React Router v6 useParams()?
What is the difference between context.go() and context.push() in go_router?
What is ShellRoute used for in go_router?
How does go_router differ from Flutter built-in named routes for web apps?