redirectUntil<T extends Object?> method

Future<T?> redirectUntil<T extends Object?>(
  1. PageRouteInfo<Object?> route, {
  2. OnNavigationFailure? onFailure,
  3. bool replace = false,
})

Temporary redirect to another route until the _completer is resolved

Calling resolver.next() or resolver.resolveNext() will remove the redirected route and mark it for replace in browser history

This is typically used like follows

onNavigation(resolver, router) { if (isAuthenticated) { resolver.next(); } else { resolver.redirectUntil(LoginRoute()); } }

Implementation

Future<T?> redirectUntil<T extends Object?>(
  PageRouteInfo route, {
  OnNavigationFailure? onFailure,
  bool replace = false,
}) async {
  if (_isRedirecting) return null;
  _isRedirecting = true;
  final result = await _router._redirect<T>(
    route,
    onFailure: onFailure,
    replace: replace,
    onMatch: (scope, match) async {
      await _completer.future;
      _isRedirecting = false;
      if (scope.stackData.any((e) => e.matchId == match.id)) {
        scope.markUrlStateForReplace();
        scope._removeRoute(match);
      }
    },
  );
  if (!_completer.isCompleted) {
    next(false);
  }
  return result;
}