Home > Back-end >  Flutter pass String variable to else
Flutter pass String variable to else

Time:09-21

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.

This is probably a very simple problem. Maybe I should use global variables?

                       onPressed: () {
                          if (Get.previousRoute == AppRoutes.home) {
                            String 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
                          }
                        },

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

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:

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:

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