Home > Software design >  Flutter GetX - How to manage controller deletion?
Flutter GetX - How to manage controller deletion?

Time:11-16

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

CodePudding user response:

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();

CodePudding user response:

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: 3);

force Will delete an Instance even if marked as permanent.

  • Related