flutterflutter-go-routergorouter

How to avoid route duplication in flutter go_router?


I have 3 categories of products laptops, desktops, mobiles. on each category page there is a list of products which on clicking shows product details page.

to pop back to category list page from product details page I've added product details page as a sub route on each one of them. So that on back press or Navigator.of(context).pop() I can go back to the relevant category list page . How to avoid this duplication ?

Routes

GoRoute( routes: [ Laptops ( routes : [ product details page] ) ] )
GoRoute( routes: [ Mobiles ( routes : [ product details page] ) ] )
GoRoute( routes: [ Desktops ( routes : [ product details page] ) ] )

three different products details page routes, share one same page (duplicates).


Solution

  • For anyone looking for answers, there's a simple technique that you can use. Idk if you should be doing this way though. (i'll explain in context of the example i've posted above, categories, products, product details)

    when going to product details page send data with it from where you're going to that page from, that contains information with it, like where were you before you visited this page and where should you go back after this.

    simple scanerio

    laptop_category > laptop_products > product_details_page

    when going from laptop_products(i.e GoRouter.go("", )) add source("laptop_products") as data either in parameters, or extra object. Then extract this data(source) on product_details_page and when going back use this "source" to go back to the page you came from (i.e laptop_products in this scanerio)