Mastersam07 / Mastersam07/kaisel

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

Open
#63 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement pkg:kaisel
Dominant language
Dart
Stars
69
Forks
2
Avg merge
18m
Merged PRs (30d)
9

Description

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

```dart
extension AppLaunchers on StackRouter {
Future launchSupport() async {
if (navigatorKey.currentContext case final context?) {
return context.read().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`.

## Workaround, and why it is unsatisfying

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

```dart
final appNavigatorKey = GlobalKey();

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

extension AppLaunchers on KaiselRouter {
Future 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`, so `extension on KaiselRouter` can reach a context the way `extension on StackRouter` could. This is also a prerequisite for a clean `maybePop` implementation — see the related issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how KaiselRouterConfig.navigatorKey is held and how KaiselRouter relates to its config or delegate. Compare this with the StackRouter usage shown in the issue, then expose the router-owned access needed for extensions on KaiselRouter to reach the navigator context without a separate top-level key.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
api, mobile
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.