5

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.

3
  • Do you mean you want close page from pages history when you are in another page? Commented Nov 5, 2020 at 12:40
  • I want to keep closing pages untill my navigator stack doesn't contain a instance of that route Commented Nov 5, 2020 at 13:27
  • off the route /someRoute * Commented Nov 5, 2020 at 15:36

7 Answers 7

12

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.

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

GetX have another useful function:

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

Close as many routes as defined by [times]

0
2

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

Navigator.of(context).popUntil(ModalRoute.withName('/route-name'));
0
Get.until((route) {
                if (route.settings.name == Routes.TEST1) {
                  //Return to the specified page
                  return true;
                } else {
                  return false;
                }
              });
1
  • 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?
    – lepsch
    Commented Sep 17, 2022 at 18:25
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.

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

of with namedRoutes:

Get.offAllNamed('/home');
1
  • That would just rerender the Homepage, which doesn't make sense. If the homepage is already there on the stack, just use it.
    – Lucifer
    Commented May 20, 2022 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.