Search code examples
flutterdartcontrollerflutter-getx

Flutter GetX - How to manage controller deletion?


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


Solution

  • 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.