Sub topic 8
Passing objects with `extra` and its limits
When to use `extra`
This chapter assumes that you know Dart null safety, can build Flutter widgets, and understand basic Navigator operations. It also assumes that you have configured GoRouter and can define a GoRoute.
A route usually receives data through its URL. A path parameter such as /books/:id is good for a small value that identifies a resource. The extra argument is different. It passes an object directly through the router in memory.
Use extra when the next screen needs an object that is already loaded:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class Book {
const Book({required this.id, required this.title});
final String id;
final String title;
}
final GoRouter router = GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/book',
builder: (context, state) {
final Object? value = state.extra;
if (value is! Book) {
return const Scaffold(
body: Center(child: Text('Book data is missing')),
);
}
return BookScreen(book: value);
},
),
],
);
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
const book = Book(id: '42', title: 'Dart in Practice');
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.push('/book', extra: book),
child: const Text('Open book'),
),
),
);
}
}
class BookScreen extends StatelessWidget {
const BookScreen({super.key, required this.book});
final Book book;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(book.title)),
body: Text('Book ID: ${book.id}'),
);
}
}The extra value is not part of /book. The router carries it alongside the location while the application is running.
The limits of `extra`
An object passed with extra is not automatically converted into a URL. This creates several limits:
- A browser cannot create a useful shareable link from the object.
- A page refresh can remove the in-memory object.
- Opening
/bookdirectly may provide noextravalue. - Android or iOS may recreate the application without the original object.
- Route state is harder to restore and test than URL data.
- Passing a large object can leave the screen with stale data.
Use a path parameter for a stable identity, then load the object again when necessary:
context.go('/books/42');Use query parameters for optional, URL-visible state such as filtering or sorting:
context.go('/books?sort=title');Use extra for temporary in-memory values, such as an already selected object or a result that does not need a public URL.
Choosing the data channel
A common design passes both an identifier and an object. The identifier supports deep links, while the object lets the first display happen immediately. The screen should still handle a missing or outdated object and reload by identifier when needed.
Exercise
A details screen must work when a user shares its URL with another person. Should its product object be passed only with extra?
Show answer
No. Put the product ID in a path parameter, such as /products/42. Use extra only as an optional in-memory shortcut, because another user or a refreshed browser may not have that object.