Flutter GetX routing history

22.4k Views Asked by At

I it possible to see the navigator stack with GetX? I looked in the documentation but I could not find anything on this subject. I usually close for example dialogs like this

Get.until((route) => !Get.isDialogOpen);

But I was wondering if I could close routes if an instance of a specific page is in the routing history which would be something like this

  Get.until((route) => !Get.routingHistory.contains('/someRoute'));

Note this isn't valid syntax.

7

There are 7 best solutions below

0
RadiantOne On

If you want to keep closing routes until you reach a page route....

Navigator.of(context).popUntil(ModalRoute.withName('/route-name'));
2
user3046679 On

You need to use:

Get.offUntil(page, (route) => false)

page means the new page to navigate.

(route) => false

Is the condition.

0
3squad On

GetX have another useful function:

int times = 2;
Get.close(times);

Close as many routes as defined by [times]

1
Jai Khambhayta On
Get.offAll(Home());  // remove all previous routes and redirect to home

of with namedRoutes:

Get.offAllNamed('/home');
0
ANAS AJI MUHAMMED On
It is possible. Navigator.popUntil pops pages until a passed predicate returns true. We can query the following route in the navigator stack and decide what decision to make. 
The GetX method for doing the same is
`

    Get.offUntil( MaterialPageRoute(builder: (context) => const NewPage()), (route) {
    var currentRoute = route.settings.name;
      debugPrint("Get.currentRoute --- $currentRoute");
        if(currentRoute  == "/Home") {
          return true;
        } else {
          return false;
        }
    }

`


The code above pops until home. Also, we can add custom logic in the if-else block above. 
0
Julien Joseph Thomas On

Get.until Remove screens until satisfying the condition. It’s the same with Navigation.popUntil(). You can use it like Get.until((route) => Get.currentRoute == '/home').

Get.offNamed By the Named route, remove the current screen and add a new screen. It’s the same with Navigation.pushReplacementNamed(). You can use it like Get.offNamed('/second').

Get.offAndToNamed By the Named route, add a new screen and then, remove the previous screen. It’s the same with Navigation.popAndPushNamed(). You can use it like Get.offAndToNamed('/second').

Get.offUntil Remove screens until satisfying the condition, and then, add a new screen. It’s the same with Navigation.pushAndRemoveUntil(). You can use it like Get.offUntil(page, (route) => (route as GetPageRoute).routeName == '/home').

Get.offNamedUntil By the Named route, remove screens until satisfying the condition, and then, add a new screen. It’s the same with Navigation.pushNamedAndRemoveUntil(). You can use it like Get.offNamedUntil(page, ModalRoute.withName('/home')).

Please use according to your usecase

1
mrliuys On
Get.until((route) {
                if (route.settings.name == Routes.TEST1) {
                  //Return to the specified page
                  return true;
                } else {
                  return false;
                }
              });