RouteController constructor

RouteController({
  1. RouteState<Object?> initialRouteState()?,
  2. RouteState<Object?> errorRouteState()?,
  3. required RouteState<Object?> fallbackRouteState(),
  4. required List<RouteBuilder<Object?>> builders,
  5. UrlStrategy urlStrategy = UrlStrategy.flat,
})

Implementation

RouteController({
  RouteState Function()? initialRouteState,
  this.errorRouteState,
  required this.fallbackRouteState,
  required List<RouteBuilder> builders,
  this.urlStrategy = UrlStrategy.flat,
}) {
  // Validation runs in BOTH debug and release. The previous version wrapped
  // these checks in `assert(() { ... return true; }())`, which is stripped
  // entirely in release builds — meaning misconfigurations could ship to
  // production with no diagnostic. Duplicate paths log a loud warning
  // (last-registration-wins is occasionally intentional). The `+`
  // delimiter under `UrlStrategy.stacked` would silently corrupt URL
  // encoding, so we throw immediately.
  final seen = <String>{};
  for (final b in builders) {
    final path = _normalizePath(b.routeState.uri);
    if (!seen.add(path)) {
      Log.err(
        'Duplicate RouteBuilder for path "$path" — last registration '
        'wins. Each route path should appear at most once in the '
        'builders list.',
      );
    }
    if (urlStrategy == UrlStrategy.stacked &&
        path.contains(RouteStackUri.delimiter)) {
      throw ArgumentError.value(
        path,
        'builders',
        'Route path contains "${RouteStackUri.delimiter}", which is '
            'reserved as the stack delimiter under UrlStrategy.stacked. '
            'Rename the route or switch to UrlStrategy.flat.',
      );
    }
  }

  _builderMap = {
    for (var builder in builders)
      _normalizePath(builder.routeState.uri): builder,
  };

  platformNavigator.addStateCallback(_handlePopState);
  resetState();

  final resolved = _resolveInitialRoutes(initialRouteState);

  _pNavigationState.set(
    _NavigationState(
      routes: resolved.routes,
      index: resolved.currentIndex,
    ),
  );
  // When cold-booting into a stack of two or more routes (typical for
  // `UrlStrategy.stacked` deep links like `/home+/sheet`), prime the
  // "previous for transition" slot with the entry directly below the
  // CURRENT index — not below the top of the list, because under
  // forward-history-aware decoding the list can extend past the current.
  // Without this, the `late` initializer on `_previousRouteForTransition`
  // assigns `currentRouteState` — i.e. the active route — on first read,
  // so `buildScreen` produces `indices = [top, top]` and the base under
  // the active route never paints.
  if (resolved.currentIndex >= 1) {
    _previousRouteForTransition = resolved.routes[resolved.currentIndex - 1];
  }
  addToCache(resolved.routes);
  // Canonicalize the browser URL to whatever stack we actually resolved.
  platformNavigator.replaceState(_encodeBrowserUri());
}