finalizeRoute method

void finalizeRoute(
  1. Route route
)

Complete the lifecycle for a route that has been popped off the navigator.

When the navigator pops a route, the navigator retains a reference to the route in order to call Route.dispose if the navigator itself is removed from the tree. When the route is finished with any exit animation, the route should call this function to complete its lifecycle (e.g., to receive a call to Route.dispose).

The given route must have already received a call to Route.didPop. This function may be called directly from Route.didPop if Route.didPop will return true.

Implementation

void finalizeRoute(Route<dynamic> route) {
  // FinalizeRoute may have been called while we were already locked as a
  // responds to route.didPop(). Make sure to leave in the state we were in
  // before the call.
  bool? wasDebugLocked;
  assert(() { wasDebugLocked = _debugLocked; _debugLocked = true; return true; }());
  assert(_history.where(_RouteEntry.isRoutePredicate(route)).length == 1);
  final int index = _history.indexWhere(_RouteEntry.isRoutePredicate(route));
  final _RouteEntry entry =  _history[index];
  // For page-based route with zero transition, the finalizeRoute can be
  // called on any life cycle above pop.
  if (entry.pageBased && entry.currentState.index < _RouteLifecycle.pop.index) {
    _observedRouteDeletions.add(_NavigatorPopObservation(route, _getRouteBefore(index - 1, _RouteEntry.willBePresentPredicate)?.route));
  } else {
    assert(entry.currentState == _RouteLifecycle.popping);
  }
  entry.finalize();
  // finalizeRoute can be called during _flushHistoryUpdates if a pop
  // finishes synchronously.
  if (!_flushingHistory) {
    _flushHistoryUpdates(rearrangeOverlay: false);
  }

  assert(() { _debugLocked = wasDebugLocked!; return true; }());
}