onExit property

ExitCallback? onExit
final

Called when this route is removed from GoRouter's route history.

Some example this callback may be called:

  • This route is removed as the result of GoRouter.pop.
  • This route is no longer in the route history after a GoRouter.go.

This method can be useful it one wants to launch a dialog for user to confirm if they want to exit the screen.

final GoRouter _router = GoRouter(
  routes: <GoRoute>[
    GoRoute(
      path: '/',
      onExit: (BuildContext context) => showDialog<bool>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('Do you want to exit this page?'),
            actions: <Widget>[
              TextButton(
                style: TextButton.styleFrom(
                  textStyle: Theme.of(context).textTheme.labelLarge,
                ),
                child: const Text('Go Back'),
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
              ),
              TextButton(
                style: TextButton.styleFrom(
                  textStyle: Theme.of(context).textTheme.labelLarge,
                ),
                child: const Text('Confirm'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
              ),
            ],
          );
        },
      ),
    ),
  ],
);

Implementation

final ExitCallback? onExit;