Flutter GETX: How to remove Initialized Controller every time we navigate to other page/routes
Asked Answered
C

8

11

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!

Callahan answered 25/4, 2021 at 7:16 Comment(1)
Not enough information is given. You need to also provide your bindings and view snippets to get proper helpMisconception
M
16

You can use:

Get.delete<FetchData>();
Musick answered 18/11, 2021 at 3:46 Comment(0)
H
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>());
Herrmann answered 26/8, 2021 at 16:12 Comment(0)
K
4

You can use

Get.deleteAll();

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

Kissel answered 25/9, 2022 at 12:34 Comment(0)
D
3

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();
Dust answered 25/4, 2021 at 11:22 Comment(0)
P
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;
   }
}
Poser answered 26/3, 2022 at 17:12 Comment(0)
H
1

You can use

Get.offAndToNamed(url)
Hostess answered 19/5, 2021 at 22:17 Comment(1)
While this code snippet may solve the question, including an explanation 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.Janeanjaneczka
R
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");
Ruffle answered 21/10, 2021 at 19:32 Comment(0)
S
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.

Swing answered 21/12, 2022 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.