Flutter GetX - How to manage controller deletion?
Asked Answered
P

3

7

I have a Flutter app with GetX Controllers. The app has 6 screens and every screen has its GetxController.

Screens 1 and 2 are for the login system, while screens 3 to 6 are for the app content.

After logging in the user can go forward and back between screens 3-4-5, but when he reaches screen 6 he can only go to Screen 3 and all the previous stacks must be deleted (so he cannot go back).

1st problem: if I do a Get.offAll(() => const Screen3()) from the Screen 6, the Controller for Screen3 gets deleted and nothing works anymore. I workaround (don't know if that word exists! :D) by marking Controller3 as permanent via

Get.put(Controller3(), permanent: true)

But here comes the

2nd problem: if the user presses the logout button (that is present only in Screen 3), this time I need the Controller3 to be deleted. This time, calling Get.offAll doesn't delete the controller, nor calling Get.delete<Controller3>(), since it says

"Controller3" has been marked as permanent, SmartManagement is not authorized to delete it.

I'm stuck in this situation and I really don't know what to do

Pleistocene answered 16/11, 2022 at 11:25 Comment(0)
B
8

So Getx as you said let us make a GetxController permanent like this:

Get.put<Controller3>.put(Controller3(), permanent: true);.

you can't delete it normally with:

Get.delete<Controller3>();

But you have the option to delete a controller that is marked with permanent, by forcing its deletion with the force property like this:

Get.delete<Controller3>(force: true);

force Will delete an Instance even if marked as permanent.

Baynebridge answered 16/11, 2022 at 13:30 Comment(2)
I ended up doing this way...though I don't really know why bindings are not working...Pleistocene
perfect solution, i was thinking it is a mistake of the GET package as I am not deleting it manually still it gets deleted, but by doing this i got the answer as well as solutions, thanksPontiff
T
2

1st problem: if I do a Get.offAll(() => const Screen3()) from the Screen 6, the Controller for the Screen3 gets deleted and nothing works anymore.

I didn't get the quoted part. When you route from 6 --> 3, the binding mechanism should generate the controller of screen 3 again.

By the way you can make it manually from anywhere using with

 var controller = Get.put(SomeController());
 controller.dispose();
Tanhya answered 16/11, 2022 at 11:50 Comment(2)
It's exactly what I thought but for some reason it's not working. I'm gonna do a deep dive!Pleistocene
If U want to re initialize the sepcific contorller that already used before, you may use “get.create”.Classroom
E
1

In my specific scenario I was using GetBuilder.

Lets suppose We have a GetxController named SomeController

Problem was that onDelete of SomeController called automatically whenever I pop screen that where SomeController was given in GetBuilder. So after some R&D I found out that there is a bool named autoRemove that have default value of true. Basically it deletes its controller whenever its context was removed from stack. Solution was simple to set it to autoRemove: false, Sample code is as follow:

GetBuilder<SomeController>(
      autoRemove: false, // set this value to false for restricting auto removal
      builder: (controller) {
        return SizedBox();
      },
    );
Encephalic answered 17/7, 2024 at 17:6 Comment(1)
I had to add this property to GetMaterialApp : smartManagement: SmartManagement.onlyBuilder, Then the 'autoremove' property worked correctly for me, thanks!Jari

© 2022 - 2025 — McMap. All rights reserved.