redirect method

FutureOr<RouteMatchList> redirect(
  1. BuildContext context,
  2. FutureOr<RouteMatchList> prevMatchListFuture, {
  3. required List<RouteMatchList> redirectHistory,
})

Processes all redirects (top-level and route-level) and returns the final RouteMatchList.

Top-level redirects are evaluated first via applyTopLegacyRedirect, then route-level redirects run on the resulting match list. If a route-level redirect changes the location, this method recurses — re-evaluating top-level redirects on the new location naturally.

Implementation

FutureOr<RouteMatchList> redirect(
  BuildContext context,
  FutureOr<RouteMatchList> prevMatchListFuture, {
  required List<RouteMatchList> redirectHistory,
}) {
  FutureOr<RouteMatchList> processRedirect(RouteMatchList prevMatchList) {
    // Step 1: Apply top-level redirect first (self-recursive for chains).
    final FutureOr<RouteMatchList> afterTopLevel = applyTopLegacyRedirect(
      context,
      prevMatchList,
      redirectHistory: redirectHistory,
    );

    // Step 2: Then apply route-level redirects on the post-top-level result.
    if (afterTopLevel is RouteMatchList) {
      return _processRouteLevelRedirects(context, afterTopLevel, redirectHistory);
    }
    return afterTopLevel.then<RouteMatchList>((RouteMatchList ml) {
      if (!context.mounted) {
        return ml;
      }
      return _processRouteLevelRedirects(context, ml, redirectHistory);
    });
  }

  if (prevMatchListFuture is RouteMatchList) {
    return processRedirect(prevMatchListFuture);
  }
  return prevMatchListFuture.then<RouteMatchList>(processRedirect);
}