Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function when returning to view/controller with `Get.back()`?

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

like image 729
KarlFri Avatar asked Oct 16 '25 10:10

KarlFri


1 Answers

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();
  }
like image 81
Arbiter Chil Avatar answered Oct 19 '25 08:10

Arbiter Chil