Search code examples
flutterflutter-getx

Using Getx Navigation Go from Homepage-> product page -> Product details page -> homepage without creating new homepage instance


If the user clicks on back from homepage, he should go to product details page.

When I am using Get.tonamed(Routes.Home) , Getx is creating a new instance of homepage.

If I remove pages in stack and go back to homepage, I am not able to take user to the details page wien the user presses the back button.

I want to take user to details page when he clicks on back button.


Solution

  • Use Get.ofAll(Home()); to avoid creating new instance of HomePage this will remove all the stacked pages in the Navigation Stack

    Explanation:

    When you go from Home -> Products -> Details -> Home.

    Navigator creates a stack, so that you can go back to the top of the stack when a page is popped

    | Home     |  👈 Top of the stack 
    | Details  |
    | Products |
    | Home     |
    |__________|
    

    What if i want only one instance of Home ?

    To access the Home you have to pop all the elements in the stack and reach Home

    1.Popping Details Screen

          _>  Pop Details Page from the stack
    |    (     |  
    | Details  |
    | Products |
    | Home     |
    |__________|
    

    2.Popping Products Screen

          _>  Pop Products Page from the stack
    |    (     |
    | Products |
    | Home     |
    |__________|
    

    Now the stack:

    
    |          |
    | Home     | 👈 Top of the stack
    |__________|
    

    Is it possible to go back to the Details Page now ?

    No, you cannot go back to DetailsPage because you have popped the DetailsPage,ProductsPage to reach Home