popRoute method

  1. @override
Future<bool> popRoute()
override

Called by the Router when the Router.backButtonDispatcher reports that the operating system is requesting that the current route be popped.

The method should return a boolean Future to indicate whether this delegate handles the request. Returning false will cause the entire app to be popped.

Consider using a SynchronousFuture if the result can be computed synchronously, so that the Router does not need to wait for the next microtask to schedule a build.

Implementation

@override
Future<bool> popRoute() async {
  NavigatorState? state = navigatorKey.currentState;
  if (state == null) {
    return false;
  }
  if (!state.canPop()) {
    state = null;
  }
  RouteMatchBase walker = currentConfiguration.matches.last;
  while (walker is ShellRouteMatch) {
    if (walker.navigatorKey.currentState?.canPop() ?? false) {
      state = walker.navigatorKey.currentState;
    }
    walker = walker.matches.last;
  }
  assert(walker is RouteMatch);
  if (state != null) {
    return state.maybePop();
  }
  // This should be the only place where the last GoRoute exit the screen.
  final GoRoute lastRoute = currentConfiguration.last.route;
  if (lastRoute.onExit != null && navigatorKey.currentContext != null) {
    return !(await lastRoute.onExit!(
      navigatorKey.currentContext!,
      walker.buildState(_configuration, currentConfiguration),
    ));
  }
  return false;
}