Skip to content

[go_router] Add StatefulShellBranches dynamically on app runtime #130386

Closed

Description

@matl-swissas

Is there an existing issue for this?

Use case

Our app consists of multiple modules. As we don't know which module is used by our users we need to create the StatefulShellBranches for our modules during runtime to preserve the state of each module and to also be able to navigate between them.

We discovered that it is possible to add new StatefulShellBranches to the StatefulShellRoute.indexedStack during runtime but they are never considered anymore during the runtime because the method "_branchLocation" of class "StatefulNavigationShellState" is only called once during the initialization. Therefore the newly added StatefulShellBranches is never added to the "_branchLocations" map and therefore the branches cannot be used.

We fixed it for our purpose by calling the method "_branchLocation" in the "build" method of the "StatefulNavigationShellState" class.

@override Widget build(BuildContext context) { final List<Widget> children = route.branches .map((StatefulShellBranch branch) => _BranchNavigatorProxy( key: ObjectKey(branch), branch: branch, navigatorForBranch: (StatefulShellBranch b) => _branchNavigators[b.navigatorKey])) .toList(); for(StatefulShellBranch branch in route.branches){ _branchLocation(branch); } return widget.containerBuilder(context, widget, children); }

It would be benefical to have a real solution for adding StatefulStateBranches during runtime.

Proposal

Add the newly added StatefulShellBranches during runtime to the "_branchLocations" map.

Activity

  1. added
    in triagePresently being triaged by the triage team
    c: new featureNothing broken; request for a new capability
    packageflutter/packages repository. See also p: labels.
    c: proposalA detailed proposal for a change to Flutter
    and removed
    in triagePresently being triaged by the triage team
    on Jul 12, 2023
  2. chutuananh2k commented on Apr 24, 2024

    @chutuananh2k

    Any progress in this issues? I face the same problem.
    @matthiaslarisch Would you mind to share an example of how to dynamic add branch at runtime?

  3. NorbertSzydlowski commented on May 13, 2024

    @NorbertSzydlowski

    I have the same problem. Any advice? @matthiaslarisch

  4. JulienElkaim commented on Oct 13, 2024

    @JulienElkaim

    Any updates? I would love to see this feature as well.

  5. bambinoua commented on Jan 14, 2025

    @bambinoua
    Contributor

    @matthiaslarisch, @dleurs, @JulienElkaim
    I think you can add new branches (=router configuration) dynamically.
    Please look at this GoRouter example.

  6. andrei-toterman commented on Jan 17, 2025

    @andrei-toterman

    @bambinoua that example shows that you are allowed to dynamically add RouteBases to the RounterConfig's routes. What we'd like to see is the capability to add StatefulShellBranches to StatefulShellRoute's branches. In that example, state is not preserved for each route.

  7. matl-swissas commented on Mar 14, 2025

    @matl-swissas
    Author

    I have created a Pull Request that fixes this issue flutter/packages#8863

  8. matl-swissas commented on Mar 19, 2025

    @matl-swissas
    Author

    see last comment on flutter/packages#8863

  9. github-actions commented on Apr 2, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Apr 2, 2025
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

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions