onEnter property

OnEnter? onEnter
final

A callback invoked for every incoming route before it is processed.

This callback allows you to control navigation by inspecting the incoming route and conditionally preventing the navigation. Return Allow to proceed with navigation or Block to cancel it. Both can optionally include an then callback for deferred actions.

When a deep link opens the app and onEnter returns Block, GoRouter will stay on the current route or redirect to the initial route.

Example:

final GoRouter router = GoRouter(
  routes: [...],
  onEnter: (BuildContext context, GoRouterState current,
            GoRouterState next, GoRouter router) async {
    if (next.uri.path == '/login' && isUserLoggedIn()) {
      return const Block.stop(); // Prevent navigation to /login
    }
    if (next.uri.path == '/protected' && !isUserLoggedIn()) {
      // Block and redirect to login
      return Block.then(() => router.go('/login?from=${next.uri}'));
    }
    return const Allow(); // Allow navigation
  },
);

Implementation

final OnEnter? onEnter;