GoRouter helps you move between screens in your Flutter app easily. It makes navigation simple and organized.
GoRouter package in Flutter
Introduction
Syntax
Flutter
final GoRouter router = GoRouter( routes: [ GoRoute( path: '/', builder: (context, state) => HomeScreen(), ), GoRoute( path: '/details', builder: (context, state) => DetailsScreen(), ), ], );
GoRouter is created with a list of GoRoute objects.
Each GoRoute has a path and a builder that returns the screen widget.
Examples
HomeScreen when the app starts.Flutter
GoRoute(
path: '/',
builder: (context, state) => HomeScreen(),
)userId from the URL to the ProfileScreen.Flutter
GoRoute( path: '/profile/:userId', builder: (context, state) { final userId = state.params['userId']!; return ProfileScreen(userId: userId); }, )
Sample App
This app starts on the Home screen with a button. When you tap the button, it navigates to the Details screen using GoRouter.
Flutter
import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { final GoRouter _router = GoRouter( routes: [ GoRoute( path: '/', builder: (context, state) => HomeScreen(), ), GoRoute( path: '/details', builder: (context, state) => DetailsScreen(), ), ], ); @override Widget build(BuildContext context) { return MaterialApp.router( routerConfig: _router, title: 'GoRouter Demo', ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home')), body: Center( child: ElevatedButton( onPressed: () => context.go('/details'), child: Text('Go to Details'), ), ), ); } } class DetailsScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Details')), body: Center(child: Text('Welcome to Details Screen!')), ); } }
Important Notes
Use context.go('/path') to navigate to a new screen.
Paths can include parameters like /profile/:id to pass data.
GoRouter works well with Flutter's MaterialApp.router for easy setup.
Summary
GoRouter makes screen navigation simple and clear.
Define routes with paths and screen builders.
Use context.go() to move between screens.
Practice
1. What is the main purpose of the
GoRouter package in Flutter?easy
Solution
Step 1: Understand GoRouter's role
GoRouter is designed to make navigation between screens easier and clearer in Flutter apps.Step 2: Compare with other options
Options A, C, and D relate to database, styling, and state management, which are not GoRouter's purpose.Final Answer:
To simplify and manage screen navigation in Flutter apps -> Option DQuick Check:
GoRouter = Navigation helper [OK]
Hint: Remember GoRouter is about moving between screens [OK]
Common Mistakes:
- Confusing navigation with state management
- Thinking GoRouter styles widgets
- Assuming GoRouter handles databases
2. Which of the following is the correct way to navigate to a new route named '/profile' using GoRouter?
easy
Solution
Step 1: Recall GoRouter navigation syntax
GoRouter usescontext.go(path)to navigate to a route.Step 2: Check each option
context.go('/profile'); usescontext.go('/profile'), which is correct. Options B, C, and D use incorrect methods or objects.Final Answer:
context.go('/profile'); -> Option CQuick Check:
Use context.go() for navigation [OK]
Hint: Use context.go() with the route path to navigate [OK]
Common Mistakes:
- Using Navigator.push instead of context.go
- Calling non-existent methods like context.navigateTo
- Trying to call goRouter.push without context
3. Given this GoRouter setup:
final router = GoRouter(
routes: [
GoRoute(path: '/', builder: (context, state) => HomeScreen()),
GoRoute(path: '/settings', builder: (context, state) => SettingsScreen()),
],
);
// In HomeScreen's button onPressed:
context.go('/settings');
What screen will be displayed after pressing the button?medium
Solution
Step 1: Analyze the routes defined
There are two routes: '/' for HomeScreen and '/settings' for SettingsScreen.Step 2: Understand the navigation call
The button callscontext.go('/settings'), which navigates to the '/settings' route.Final Answer:
SettingsScreen -> Option BQuick Check:
context.go('/settings') shows SettingsScreen [OK]
Hint: context.go(path) shows the screen for that path [OK]
Common Mistakes:
- Assuming context.go('/') stays on HomeScreen
- Thinking navigation causes error without extra setup
- Confusing builder with route path
4. What is wrong with this GoRouter route definition?
GoRoute( path: '/profile', builder: (context) => ProfileScreen(), )
medium
Solution
Step 1: Check GoRoute builder signature
The builder function must accept two parameters: context and state.Step 2: Identify the error
The code only has one parameter (context), missing the required 'state' parameter.Final Answer:
The builder function is missing the second parameter 'state' -> Option AQuick Check:
builder(context, state) needs two parameters [OK]
Hint: Builder always needs (context, state) parameters [OK]
Common Mistakes:
- Omitting the 'state' parameter in builder
- Thinking path must be capitalized
- Assuming name property is mandatory
- Wrapping screen in MaterialApp inside builder
5. You want to pass a user ID as a parameter in the route path using GoRouter. Which route definition and navigation call are correct?
// Route definition options:
A) GoRoute(path: '/user/:id', builder: (context, state) => UserScreen(id: state.params['id']!))
B) GoRoute(path: '/user', builder: (context, state) => UserScreen(id: state.params['id']!))
// Navigation call options:
1) context.go('/user/123');
2) context.go('/user?id=123');hard
Solution
Step 1: Understand route parameter syntax
Route path with ':id' means the ID is part of the path, so '/user/:id' is correct for path parameters.Step 2: Match navigation call to route
To pass the ID in the path, navigation must be like '/user/123', so call 1 matches route A.Final Answer:
Use route A with navigation call 1 -> Option AQuick Check:
Path param ':id' needs '/user/123' navigation [OK]
Hint: Use ':param' in path and '/value' in go() call [OK]
Common Mistakes:
- Using query parameters with path param syntax
- Missing ':id' in route path
- Passing ID as query string when route expects path param
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
