didRemove method

  1. @override
void didRemove(
  1. Route route,
  2. Route? previousRoute
)
override

The Navigator removed route.

If only one route is being removed, then the route immediately below that one, if any, is previousRoute.

If multiple routes are being removed, then the route below the bottommost route being removed, if any, is previousRoute, and this method will be called once for each removed route, from the topmost route to the bottommost route.

Implementation

@override
void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
  RouteInfo? routeName = _findRouteByName(routeName: route.settings.name ?? '');

  if (routeName != null) {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      NavigationInterface navigation = GetIt.instance<NavigationInterface>();
      RouteInfo? previousRouteInfo = _findRouteByName(routeName: previousRoute?.settings.name ?? '');
      if (routeName.isShellRoute) {
        //disposing children of the shell route because they are not disposed automatically.
        navigation.stack.sublist(0, navigation.stack.length - 1).reversed.forEach((element) {
          navigation.registeredControllers[element]?.onDispose();
        });
        //keeping the last element in the stack because it is the new page that is navigated to.
        navigation.stack = [navigation.stack.last];
      }
      navigation.disposeRoute(
        previousRoute: previousRouteInfo,
        poppedRoute: routeName,
        updateStack: !routeName.isShellRoute,
      );
    });
    log('Removing ${routeName.name}, previous is ${previousRoute?.settings.name}');
  }
}