7

newbie here. How do I re-run onInit() every time I push back to my screen? onInit() runs only once but navigating back to a previous screen does not delete the controller which was initialized (FetchData) hmmm..

I'm only using Get.back() every time I want to pop page, and Get.toNamed() every time I want to navigate on a named route

the only thing I want to happen is to delete the Initialized controller (FetchData) every time I pop the page but I have no Idea how to do it.

my GetxController

class FetchData extends GetxController {
    RxList items = [].obs;
    @override
    onInit() {
      fetchData();
      super.onInit();
    }
    
    Future<void> fetchData() async {
     var result = await http.get("api.url");
     items.value = result.body;
    }
}

Thanks in advance!

Koala
  • 352
  • 2
  • 7
  • 18

8 Answers8

9

You can use:

Get.delete<FetchData>();
Airshu
  • 155
  • 1
  • 5
7

When I logout Get.off, Get.offUntil, Get.offAndToNamed methods doesnt remove my GetXController from memory.

then I tried below code and everything works fine.

 Get.offUntil(GetPageRoute(page: () => Login()), ModalRoute.withName('toNewLogin') );
   
     Timer(Duration(milliseconds: 300), ()=>Get.delete<MainPageController>());
Bilal Şimşek
  • 5,453
  • 2
  • 19
  • 33
2

The onInit is only called once. You can use another method to run when back from another screen, for example, when call the new screen you can await until it closes and then call your method again:

//go to new screen
await Get.toNamed(screenName);
//after run my method
controller.fectchData();

if you want call the method only in some cases you can pass a bool back to ask if needs reload:

Get.back(result: true);

and in the screen that called:

//go to new screen
final result = await Get.toNamed(screenName);
    
if(result != null && result == true)//after run only if needed
controller.fectchData();
Mahmoud Salah Eldin
  • 1,739
  • 16
  • 21
Jorge Vieira
  • 2,784
  • 3
  • 24
  • 34
2

You cannot put method fetchData() on super.onInit(). When you use Get.offAllName(), Get.offAndToName(), Get.offAll(), etc... Method fetchData() is still kept in memory => Cannot dispose or close it.

FIX:

class FetchData extends GetxController {
   RxList items = [].obs;
   @override
   onInit() {
     super.onInit(); // <--- swap code here
     fetchData(); // <--- swap code here
   }

   Future<void> fetchData() async {
      var result = await http.get("api.url");
      items.value = result.body;
   }
}
Huu Bao Nguyen
  • 1,051
  • 2
  • 14
  • 40
1

You can use

Get.offAndToNamed(url)
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Jeroen Steenbeeke May 20 '21 at 07:01
1

You can use

Get.deleteAll();

This will remove all the Initialized controllers instances from your memory.

Chand Abdullah
  • 429
  • 6
  • 13
0

Use Get.offAllNamed. It will remove all controllers and create only the final destination route controller. Tested with get: ^4.3.8

        Get.offAllNamed("your final route");
Ares91
  • 506
  • 2
  • 8
  • 27
0

The simple answer is Get.delete<FetchData>(); if you had created like Get.put(FetchData())

But If you have created like Get.put(FetchData(), permanent: true); or by LazyPut you can delete that by forcing Get.delete<FetchData>(force: true);

I hope this helps you.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85