Sub topic 4
Navigating: go, push and pop
Prerequisites
This chapter assumes that you know basic Dart null safety, can build Flutter widgets, and have used Navigator.push and Navigator.pop. It also assumes that your app already uses GoRouter with MaterialApp.router.
In go_router, navigation starts with a location. A location is a URL-style path such as / or /books/42. The router matches that location against the route tree and builds the required pages.
`go` replaces the route stack
Use context.go when the new location represents the app's current destination. The router rebuilds the page stack for that location.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/',
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
routes: <RouteBase>[
GoRoute(
path: 'details',
builder: (context, state) => const DetailsScreen(),
),
],
),
],
);
void main() {
runApp(
MaterialApp.router(
routerConfig: router,
),
);
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/details'),
child: const Text('Open details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: Center(
child: ElevatedButton(
onPressed: () {
if (context.canPop()) {
context.pop();
}
},
child: const Text('Back'),
),
),
);
}
}go is useful for primary navigation, such as switching between Home, Search, and Settings. It is also useful after login, when the app should move to a new destination instead of preserving the login screen.
`push` adds a page
Use context.push when the new screen is a temporary step above the current screen. The current page remains below it, so pop can reveal that page again.
For example, a list can push a detail screen:
onTap: () => context.push('/books/42');Use push for details, forms, previews, and dialogs represented as routes. Use go when the destination should define the app's navigation state.
The exact result depends on your route tree. A nested route can preserve its parent page, while a separate top-level location may replace the matched stack.
`pop` removes the current page
context.pop() removes the top route from the navigation stack. Check context.canPop() when the action might run at the root. On web, browser back navigation also changes the location, and go_router updates the displayed pages to match it.
Exercise
Exercise
A settings screen opens an account editor, and the user should return to settings after saving or cancelling. Should the editor use go or push to open, and which method should return to settings?
Show answer
Use context.push to open the editor and context.pop to return to settings. Use go when changing the app's primary destination instead of adding a temporary page.