[go_router] Limit navigation stack

Hello everyone,

I have an existing Flutter app that uses go_router for navigation, and I’d like to implement a limit on the navigation stack size.

Specifically, I want to push new pages onto the stack as usual, but once the stack reaches a certain limit (for example, 20 screens), I want to automatically remove the oldest route — essentially implementing a FIFO (First In, First Out) navigation behavior.

Here’s a simple example to illustrate what I mean:
[Home] → [Home, A] → [Home, A, B] → [Home, A, B, C] → [Home, A, B, C, D] → [ limit reached ] → [Home, B, C, D].
Also i need to mention that [Home] is initially a tab in bottom navigation bar and when I navigate back from [B, C, D] it should navigate to [Home] tab.

Could you please suggest an approach or share an example of how to achieve this using go_router?

For home always being the root, I think you can use a nested navigator and that should be solvable.

For the queue part, I dont think go router exposes a way to remove the bottom most part of the stack, but if you observe changes via (.routeInformationProvider.didPushRouteInformation), and also maintain the RouteMatchList in app state - you should be able to always use go instead of push and replicate a similar effect by manually modifying what you go to

1 Like