navigate method

  1. @override
Future<void> navigate(
  1. T route
)
inherited

Navigates to a route with smart history handling.

If route exists in stack, pops back to it. Otherwise pushes new route. Used for browser back/forward navigation.

Implementation

@override
Future<void> navigate(T route) => runNavigationTransaction(() async {
  final target = await RouteRedirect.resolve(route, this);
  if (target == null) return;

  final parentLayout = target.resolveParentLayout(this);
  if (parentLayout != null) {
    await prepareParentLayoutList(
      parentLayout,
      strategy: _ResolveLayoutStrategy.pushToTop,
    );
  }

  final parentPath = parentLayout?.resolvePath(this) ?? root;

  assert(
    parentPath is StackNavigatable,
    'ZenRouter: parentPath (${parentPath.pathKey.key}) does not implement '
    'StackNavigatable. The navigate() call for route $route will have no '
    'effect on the navigation stack or browser history.',
  );

  switch (parentPath) {
    case final StackCommit<T> commit:
      recordHistoryIntent(
        parentPath.stack.contains(target)
            ? NavigationHistoryIntent.replace
            : NavigationHistoryIntent.push,
      );
      await commit.commitResolvedNavigate(target);
    case final StackNavigatable navigable:
      recordHistoryIntent(NavigationHistoryIntent.push);
      await navigable.navigate(target);
  }
});