resolveParentLayout method

  1. @override
RouteLayout<RouteUnique>? resolveParentLayout(
  1. covariant CoordinatorCore<RouteUri> coordinator
)
override

Resolves the active or creates a new RouteLayoutParent for this route.

This is the primary method for finding where a route belongs in the navigation hierarchy. It checks the coordinator's currently active layouts first, returning an existing instance if found. If no matching layout exists, it creates a new one via createParentLayout.

Returns null if parentLayoutKey is not set.

Implementation

@override
RouteLayout? resolveParentLayout(coordinator) {
  final layout = _proxy.resolveParentLayout(coordinator) as RouteLayout?;

  // Validate that routes using fixed-membership paths are declared upfront.
  // Using assert with closure to ensure all validation logic is removed in production
  assert(() {
    final p = layout?.resolvePath(coordinator);
    if (p is BranchedStackPath) {
      final path = p as BranchedStackPath;
      final routeInBranches = path.stack.any(
        (route) => route.runtimeType == runtimeType,
      );
      if (!routeInBranches) {
        throw AssertionError(
          'Layout [$runtimeType] resolves under a BranchedStackPath but is '
          'not declared as a branch root.\n'
          'BranchedStackPath: ${path.debugLabel ?? 'unlabeled'}\n'
          'Current branches: '
          '${path.stack.map((route) => route.runtimeType).toList()}\n\n'
          'Fix: add [$runtimeType] as a branch layout when creating the path:\n'
          '  BranchedStackPath.createWith(\n'
          '    [...existing branches..., $runtimeType()],\n'
          '    coordinator: this,\n'
          "    label: '${path.debugLabel ?? 'your-label'}',\n"
          '  )',
        );
      }
      return true;
    }
    if (p is IndexedStackPath) {
      final path = p as IndexedStackPath;
      final routeInStack = path.stack.any(
        (r) => r.runtimeType == runtimeType,
      );
      if (!routeInStack) {
        throw AssertionError(
          'Route [$runtimeType] uses an IndexedStackPath layout but is not present in the initial stack.\n'
          'IndexedStackPath: ${path.debugLabel ?? 'unlabeled'}\n'
          'Current stack: ${path.stack.map((r) => r.runtimeType).toList()}\n\n'
          'Fix: Add an instance of [$runtimeType] to the IndexedStackPath when creating it:\n'
          '  IndexedStackPath.createWith(\n'
          '    [...existing routes..., $runtimeType()],\n'
          '    coordinator: this,\n'
          '    label: \'${path.debugLabel ?? 'your-label'}\',\n'
          '  )',
        );
      }
    }
    return true;
  }());

  return layout;
}