3

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.

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112

7 Answers7

8

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

6

You need to use:

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

page means the new page to navigate.

(route) => false

Is the condition.

  • 1
    what is the first Argument for Get.offUntil(, (route) => false) or how to navigate to new page? I directly want to pass a class name at and don't want to use named route as arguments. is it possible? – Dhiren Basra Mar 08 '21 at 05:56
  • 1
    We can use in this way: RaisedButton( child: Text("offUntil Fourth"), onPressed: () => Get.offUntil( MaterialPageRoute( builder: (context) => Second(), maintainState: false), (route) => false)), – webaddicted Nov 12 '21 at 04:44
4

GetX have another useful function:

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

Close as many routes as defined by [times]

3squad
  • 355
  • 3
  • 14
2

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

Navigator.of(context).popUntil(ModalRoute.withName('/route-name'));
RadiantOne
  • 189
  • 5
0
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
Get.until((route) {
                if (route.settings.name == Routes.TEST1) {
                  //Return to the specified page
                  return true;
                } else {
                  return false;
                }
              });
  • 2
    Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than the existing answers. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – lepsch Sep 17 '22 at 18:25
-1
Get.offAll(Home());  // remove all previous routes and redirect to home

of with namedRoutes:

Get.offAllNamed('/home');
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
  • That would just rerender the Homepage, which doesn't make sense. If the homepage is already there on the stack, just use it. – Lucifer May 20 '22 at 12:11