Flutter, conditional navigation / routing using GetX

Viewed 41

Lets say that there is a way to enter the same screen from different places:

home screen -> final screen

second screen -> final screen

third screen -> final screen

etc...

how can I know from which screen I entered from? Because I need to make my own back button (default back button doesn't reset the state of the previous screen, and the state needs to reset ) I can't seem to find the example for the problem.

Can you use if statement for that?

Also I'm using GetX package

Desired outcome:

home screen -> final screen (back button)-> home screen

second screen -> final screen (back button)-> second screen

third screen -> final screen (back button)-> third screen

3 Answers

The get package allows Get.back() to navigate back to the screen from where the final screen was opened.

Navigating from homeScreen to final Screen

//using route
Get.toNamed('/finalScreen'); 
//or
Get.to(() => FinalScreen());

Use Get.back(); to you navigate to homeScreen

Same in other cases

Instead of Get.back() or Get.toNamed(NameOfTheScreen), use Get.toNamed(Get.previousRoute), it works perfectly. It navigates to the correct previous screen, and resets the state of that screen

Related