Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[go_router] Feature Request: Expose observers parameter for StatefulShellRoute to support RouteAware in nested navigators #168074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jimmyff opened this issue Apr 30, 2025 · 1 comment
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter p: go_router The go_router package package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team

Comments

@jimmyff
Copy link

jimmyff commented Apr 30, 2025

StatefulShellRoute provides a way to implement tabbed navigation with preserved state for each branch (shell). However, I can't find a way to attach NavigatorObserver instances (like RouteObserver) specifically to the nested Navigator widget(s) it manages internally for its branches.

This is a problem when trying to use standard Flutter patterns like the RouteAware mixin on pages within a StatefulShellRoute branch. RouteAware relies on ModalRoute.of(context) to find its associated route and navigator, and then subscribes to a RouteObserver associated with that navigator.

If a RouteObserver is only added to the top-level GoRouter constructor's observers list, it only observes the root navigator. Navigation events happening within the StatefulShellRoute's nested navigator (e.g., pushing a detail page onto a branch's initial page) are not seen by the global observer. Consequently, RouteAware methods (didPush, didPop, didPopNext, didPushNext) do not get called for pages managed by the StatefulShellRoute's navigator.

Example Scenario:

  1. App uses StatefulShellRoute for bottom navigation (e.g., Tabs A, B, C).
  2. Tab A's initial route is /a.
  3. From /a, the user navigates to /a/detail. The DetailPage uses RouteAware.
  4. A global RouteObserver is added via GoRouter(observers: [myGlobalObserver]).
  5. The DetailPage subscribes using myGlobalObserver.subscribe(this, ModalRoute.of(context)!).
  6. Problem: The ModalRoute.of(context) correctly finds the route within the shell's navigator, but myGlobalObserver is not attached to that specific navigator, so the RouteAware callbacks on DetailPage never fire when navigating to/from it within Tab A.

Proposed Solution:

Add an observers parameter to the StatefulShellRoute definition (and consequently to the TypedStatefulShellRoute annotation helper). This parameter would accept a List.

// Example using annotations (Conceptual)
@TypedStatefulShellRoute<AuthenticatedShellRouteData>(
  // <<< Proposed Parameter >>>
  observers: [shellRouteObserver], // Allow passing observers here
  branches: <TypedStatefulShellBranch<StatefulShellBranchData>>[
    // ... branches ...
  ]
)
class AuthenticatedShellRouteData extends StatefulShellRouteData {
  // ...
}

// Or directly on StatefulShellRoute (Conceptual)
StatefulShellRoute(
  builder: ...,
  branches: ...,
  // <<< Proposed Parameter >>>
  observers: [shellRouteObserver],
  navigatorContainerBuilder: ...,
)

These observers would then be attached to the nested Navigator widget(s) created and managed by the StatefulShellRoute for its branches.

@darshankawar darshankawar added in triage Presently being triaged by the triage team c: new feature Nothing broken; request for a new capability package flutter/packages repository. See also p: labels. c: proposal A detailed proposal for a change to Flutter p: go_router The go_router package team-go_router Owned by Go Router team and removed in triage Presently being triaged by the triage team labels May 2, 2025
@chunhtai
Copy link
Contributor

chunhtai commented May 8, 2025

They are in the StatefulShellBranch because every branch creates a separate navigator. Closing as WAI

@chunhtai chunhtai closed this as completed May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter p: go_router The go_router package package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team
Projects
None yet
Development

No branches or pull requests

3 participants