In my app, I have a HomePage
that displays a few selected posts and a PostListPage
. From both pages I open posts.
I have this route structure.
GoRouter(
routes: [
GoRoute(
path: "/",
builder: (context, state) => HomePage(),
routes: [
GoRoute(
path: "post/:id",
builder: (context, state) => // ...
),
GoRoute(
path: "posts",
builder: (context, state) => PostListPage()
)
],
),
],
);
When I open a post one the home page with GoRouter.go
, it works as intended but when I open a post from the PostListPage, the back button routes back to the HomePage, when it should route back to the PostListPage.
How can I fix this?
I know I can use GoRouter.push
instead, however, with push, on web, the route is not reflected in the URL and therefore, when refreshing the browser on a PostPage, it starts the app at the HomePage.
I could use the GoRouter.optionURLReflectsImperativeAPIs
option to avoid this URL issue, but the docs say "It is strongly suggested against setting this value to true".
If you want to have a
post/:id
route from the home and the post list pages as well, you can add a child route underposts
:When navigating from home, use:
And from the post list:
After this navigating back should word as intended.
This is a simple example that you can run in DartPad: