Skip to content

feat: expose navigatorKey on KaiselRouter so extensions on the router can reach a BuildContext #63

Description

@Mastersam07

auto_routes StackRouter carries navigatorKey. Apps use that to reach a BuildContext from a router-typed receiver:

extension AppLaunchers on StackRouter {
  Future<void> launchSupport() async {
    if (navigatorKey.currentContext case final context?) {
      return context.read<SupportLauncher>().open();
    }
  }
}

This pattern is often load-bearing rather than incidental. The router receiver acts as a capability gate: app services are reachable only from somewhere that legitimately holds a router — i.e. a routing layer — instead of from any widget in the tree. Moving those extensions onto BuildContext would make the services callable everywhere, which is usually the thing the design was preventing.

KaiselRouterConfig exposes navigatorKey, but KaiselRouter does not, and the router has no route back to its config or delegate. So such extensions cannot be ported by retyping the receiver from StackRouter to KaiselRouter<R>.

Workaround, and why it is unsatisfying

Hoist the key to a top-level final and hand it to the config:

final appNavigatorKey = GlobalKey<NavigatorState>();

final config = KaiselRouterConfig<AppRoute>(navigatorKey: appNavigatorKey, /* ... */);

extension AppLaunchers on KaiselRouter<AppRoute> {
  Future<void> launchSupport() async {
    if (appNavigatorKey.currentContext case final context?) { /* ... */ }
  }
}

Works, but the key is now app state rather than router state, and there are two sources of truth if anyone also passes a key to the config.

Suggestion

Expose navigatorKey (or the owning delegate) on KaiselRouter<R>, so extension on KaiselRouter<R> can reach a context the way extension on StackRouter could. This is also a prerequisite for a clean maybePop implementation — see the related issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpkg:kaiselThe kaisel Flutter package

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions