setNewRoutePath method

  1. @override
Future<void> setNewRoutePath(
  1. RouteMatchList configuration
)
override

For use by the Router architecture as part of the RouterDelegate.

Implementation

// This class avoids using async to make sure the route is processed
// synchronously if possible.
@override
Future<void> setNewRoutePath(RouteMatchList configuration) {
  if (currentConfiguration == configuration) {
    return SynchronousFuture<void>(null);
  }

  assert(configuration.isNotEmpty || configuration.isError);

  final BuildContext? navigatorContext = navigatorKey.currentContext;
  // If navigator is not built or disposed, the GoRoute.onExit is irrelevant.
  if (navigatorContext != null) {
    final int compareUntil = math.min(
      currentConfiguration.matches.length,
      configuration.matches.length,
    );
    int indexOfFirstDiff = 0;
    for (; indexOfFirstDiff < compareUntil; indexOfFirstDiff++) {
      if (currentConfiguration.matches[indexOfFirstDiff] !=
          configuration.matches[indexOfFirstDiff]) {
        break;
      }
    }
    if (indexOfFirstDiff < currentConfiguration.matches.length) {
      final List<GoRoute> exitingGoRoutes = currentConfiguration.matches
          .sublist(indexOfFirstDiff)
          .map<RouteBase>((RouteMatch match) => match.route)
          .whereType<GoRoute>()
          .toList();
      return _callOnExitStartsAt(exitingGoRoutes.length - 1,
              navigatorContext: navigatorContext, routes: exitingGoRoutes)
          .then<void>((bool exit) {
        if (!exit) {
          return SynchronousFuture<void>(null);
        }
        return _setCurrentConfiguration(configuration);
      });
    }
  }

  return _setCurrentConfiguration(configuration);
}