Home > Mobile >  Flutter GetX complex Navigation
Flutter GetX complex Navigation

Time:09-22

I want to assign the previous Route to String help, but I can't use that variable in the last else statement, just getting an Undefined name error.

EDIT:

The problem is regarding navigation, Im using GetX for navigation, and the problem is that using Get.back() isn't refreshing the state of the page that I want to get back to, so I need to use Get.toNamed.

This onPressed is used on my back button (on "screen2")

I can go to "screen2" page from "home", "screen1" and "screen3".

"home" and"screen1" are parent screens to "screen2" (is you can say it like that), but "screen3" is only accessed from "screen2".

Now, when I return to "screen2" from "screen3", GetX saves the previous route of "screen3"

and when I press the back button(on "screen2") , it should either go to "home" or "screen1", depends on from where I opened "screen2"

It's pretty simple, but it's hard to explain in a simple way

enter image description here

EDIT: The upper part was easy, but now, there is the final boss: I believe that those below are all the routes,

the problem is probably around about path screen, cause it is the part where the most data goes through

Home -> about mountain,
Home -> about path,
Home -> about poi,
Home -> about mountain -> about path 
Home -> about mountain -> about path -> about poi
Home -> mountains screen -> about mountain -> about path
Home -> mountains screen -> about mountain -> about path -> about poi
Home -> paths screen -> about path
Home -> paths screen -> about path -> about poi

enter image description here

"screen2" is the same as "ABOUT MOUNTAIN"

I don't know if there is a better way of doing this other that using a bunch of conditionals.

enter image description here (this is clearly wrong, there must be a more elegant approach)

CodePudding user response:

define help var in onpressed function.try this:

    onPressed: () {
final String help='';
                          if (Get.previousRoute == AppRoutes.home) {
                            help = Get.previousRoute.toString();
                            Get.toNamed(AppRoutes.home);
                          } else if(Get.previousRoute == AppRoutes.screen2){
                            String help = Get.previousRoute.toString();
                            Get.toNamed(AppRoutes.screen2);
                          } else{
                            Get.toNamed(help);  //can't use variable help here
                          }
                        },

CodePudding user response:

Try adding argument returnUrl which contains home or Screen1 to your url to screen2 and pass the same argument to Screen3.

Get the argument returnUrl using

Get.arguments['returnUrl'];

Use the argument value to route from screen2 to home/screen1

CodePudding user response:

I have read your comments and found that you need a previous route on another page my suggestion for you is that just store the previous route in local storage using the shared_preferences package and call that route on a particular variable once the page pop out then delete that route from storag.

https://pub.dev/packages/shared_preferences

  • Related