Sub topic 1
From Navigator.push to declared routes
Flutter navigation can be written in two styles. Imperative navigation tells the app what action to perform now, such as “push this screen.” Declarative routing describes which screen belongs to a location, such as /books/42. The router then builds the navigation state that matches that location.
This chapter assumes Dart null safety, basic Flutter widgets, Navigator.push and Navigator.pop, and a basic understanding of ChangeNotifier.
Prerequisites
You should be able to create a Flutter screen with StatelessWidget or StatefulWidget. You should also understand that a navigation stack contains the current screen and the screens beneath it.
Imperative navigation with Navigator
With the imperative style, a button directly changes the navigation stack. The current widget chooses the next widget and passes it to Navigator.push.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HomeScreen()));
}
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: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context) => const DetailsScreen(),
),
);
},
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: () => Navigator.of(context).pop(),
child: const Text('Back'),
),
),
);
}
}This approach is simple for small flows. However, the destination is hidden inside a callback. A web URL, a notification, or a restored app state must separately determine which widgets to place on the stack.
Declarative routing with locations
A declarative router maps locations to screens. A location is a URL-like string, such as / or /books/42. In go_router, a GoRoute declares that mapping.
The important difference is the source of truth:
| Style | Source of truth | Typical operation |
|---|---|---|
| Imperative | The sequence of navigation commands | `push` or `pop` |
| Declarative | The current location and route tree | Match `/details` to a route |
With a router, changing the location to /details produces the details screen. The same location can come from a button, a deep link, browser history, or restored application state. This makes navigation easier to connect to external events.
Why the distinction matters
Imperative code is useful when a user completes a short flow and you need to add or remove one page. Declarative routing is more useful when screens need stable URLs, authentication rules, nested navigation, or browser support.
The styles also differ when application state changes. An imperative app may need to inspect the stack and remove pages manually after logout. A declarative router can reevaluate its route rules and select /login when the user is signed out.
Declarative routing does not remove the idea of a stack. A router still creates pages and handles back navigation. It changes where the navigation decision lives: route declarations and application state describe the desired result, while the router performs the transition.
Exercise
A notification should open book 42 when the app starts, and the same screen must work from a browser URL. Which style is the better foundation, imperative Navigator calls or declarative routing, and why?
Show answer
Declarative routing is the better foundation because /books/42 can be produced by the notification or browser, and the route declaration maps that location to the book screen.