csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 97 forks source link

Optional path parameters #324

Closed adamsowinski closed 2 years ago

adamsowinski commented 2 years ago

Hello I have an admin area of my app that uses a bottom tabbar for navigation. I'm using a nested navigation for it and it works great (route '/admin/:bottomBarIndex/'). On one page I have two tabs that are placed below app bar. What I would like to achive is have a nested navigation route like this '/admin/:bottomBarIndex/:topBarIndex' where topBarIndex parameter would be optional cause it applies only for one bottomBarIndex. How to do it? It seems that with current setup only routes with two parameters are matched so for example: '/admin/2/1' and not '/admin/2'.

csells commented 2 years ago

Path parameters are required. You can use query parameters for optional params. Or you can use direction, e.g.

routes: [
  // fill in the default value for topBarIndex
  GoRoute(path: '/admin/:bottomBarIndex', redirect: (c,s) => '/admin/${s.params['bottomBarIndex']}/1'),

  // now you have both params
  GoRoute(path: '/admin/:bottomBarIndex/:topBarIndex', build: (c,s) ...,
]