Migrating to 3.0
go_router v3.0 comes with two breaking changes:
- signature of the
navigatorBuilder
function - behavior and signature of the
GoRouter.pop
function
Change to the GoRouter.pop
function behavior and signature
In previous versions, the GoRouter.pop
function simply called Navigator.pop
.
This was somewhat non-intuitive, since, unlike the rest of the GoRouter
methods, it required a BuildContext
parameter:
// global GoRouter
final _router = GoRouter(...);
...
TextButton(
onPressed: () async {
if (await abandonNewPerson(context)) {
_router.pop(context); // pass the context, please
}
},
child: const Text('Cancel'),
),
Some developers prefer to make their GoRouter
instance global so that they can
call them without the use of a context, which meant they couldn't call pop
.
This was done as a simple way to implement GoRouter.pop
by simply forwarding
to Navigator.pop
, which itself was non-intuitive; go_router has it's own stack
of pages, calling pop
should just work with those pages, right?
Either way, you can continue to call Naviator.pop(context)
if that's the
behavior you want. Otherwise, you can use the simpler version of GoRouter.pop
:
// global GoRouter
final _router = GoRouter(...);
...
TextButton(
onPressed: () async {
if (await abandonNewPerson(context)) {
_router.pop(); // no context for you!
}
},
child: const Text('Cancel'),
),
If you do have context and are using the Dart extension, your code won't need to change at all:
TextButton(
onPressed: () async {
if (await abandonNewPerson(context)) {
context.pop(); // no code changes needed in this case
}
},
child: const Text('Cancel'),
),