Home > Blockchain >  Flutter Delete Getx Controller through variable
Flutter Delete Getx Controller through variable

Time:10-04

I need to remove and put GetxController to run all its default function like onInit, onReady .

This work fine with Get.delete<MyHomeController>();

I need this command with variable reference like

String myControllerName = 'MyHomeController';
Get.delete< myControllerName>();

Can I do something like this?

Thanks in Advance.

CodePudding user response:

This function cannot set a string in as you can see in the structure below

Future<bool> delete<S>({String? tag, bool force = false})

CodePudding user response:

I'm not sure if there's easier way to do it, but one option is just to make a helper method that lists all controllers you have, something like this:

  void delete(String s) {
    switch (s) {
      case 'MyHomeController':
        Get.delete<MyHomeController>();
        break;
      case 'AnotherController':
        Get.delete<AnotherController>();
        break;
      case 'ThirdController':
        Get.delete<ThirdController>();
        break;
    }
  }
  • Related