2

I am switching from the home view to another using Get.toNamed(Routes.DETAIL). When I want to return from the details view to the home view, I am calling Get.back() (or the user is using the back button of the devices). Back on the home view, I would like to fetch all data from my database again.

Is there any function that is triggered when I am leaving a few and returning to it, so I can put my logic there?

Thank you

KarlFri
  • 23
  • 1
  • 5
  • Instead of going back to a view you can go back to an action which fetches data from database and returns "Home" view; – maryam mohammadbagheri Jul 17 '22 at 12:13
  • Ah yes, with `await` I could wait for the return. But I would prefer a more generic solution, otherwise I have to do this for every button I add that redirect the user to another view. – KarlFri Jul 17 '22 at 12:22

3 Answers3

3

I would suggest you to use Get.offNamed() instead of Get.toNamed() as the offNamed() function will clear the data stored in catch and thus will again call the API declared in onInit() or onReady() lifecycle when returning back to that screen.

Snehal Singh
  • 116
  • 4
0

In Getx they have a funtion like

Get.back(result:"result");

so in order to trigger some funtion when going back to any page route

try doing this as the document written

final gotoHome = await Get.toNamed(Route.name); // or use the simple one Get.to(()=> Home());

then if you trigger to go back in page you should indicate some result e.g.

from back button in phone using willpopscope or a back button in UI.

Get.back(result:"triggerIt"); // this result will pass to the home.

so in will use

   // It depend on you on where you gonna put this
  // onInit or onReady or anything that would trigger
    someTrigger() async{
    final gotoHome = await Get.toNamed(Route.name);
      if(gotoHome == "triggerIt"){
          anyFuntionYouwantoTrigger();
       } 
    }

for more info about it try to read the documentation. https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md

Edited: // Maybe some answer will pop up for better I do have one but it's not that quite a real practice just a sample

e.g // sample you are now in the current page and this page is also connected to homecontroller or using Get.find() it need to bind the controller to the page;

class BindingHome with Bindings{     
  @override
  void dependencies() {
   Get.lazyPut(() => HomeController(), fenix: true);
   } 
}

then from GetPage add Binding

 GetPage(
   name: "/currentpage",
   binding: BindingHome(),
   page:() => HomeView(),
  ),

so while homecontroller is bind to the current page you are now so

// lets assume this one is put to the CurrentController
final homeController = Get.find<HomeController>();

so while calling back button on ui or willpopscope when back try to trigger the function from home

  gotBackfunction(){
    Get.back();
    homeController.anyFuntionYouwantoTrigger();
  }
Arbiter Chil
  • 1,061
  • 1
  • 6
  • 9
  • Thank you for the detailed example. Is this the only way? I hoped for some function that is automatically called in the lifecycle of the view when I navigate back to the view. – KarlFri Jul 17 '22 at 12:34
  • have you tried binding? i edited it, This is also other way – Arbiter Chil Jul 17 '22 at 12:46
  • But this also does not work when someone presses the back button on their phone. (The first method works, but I thought there would be a lifecycle function) – KarlFri Jul 17 '22 at 13:04
  • The first solution is an ok choice, but the example is unnecessarily long. The essence is: `Get.toNamed("Route").then(reloadMyData);` You don't need to pass a result value to do this. – jakobleck Feb 24 '23 at 14:14
0

No you can't really call a function when doing so.

You should be using callback function just before popping the view.

onBackClick() async {
    Get.lazyPut<MainController>(
        () => MainController(),
    );
    controller.allItems.refresh();
    Get.back();
}

Here is an example how you can do it without any complications:

WillPopScope(
      onWillPop: () async {
        await onBackClick();
        return Future(() => false);
      },
      child: Scaffold(
        appBar: CustomAppBarWithBack(
          title: "Appbar",
          OnClickBack: onBackClick,
        ),
        body: Widget(),
      ),
    );