Search code examples
flutterdartgetstateflutter-getx

How to get current tag inside controller? - Flutter GetX


I created a controller with tag, i need to access that tag inside controller, is it possible?

This is how i put controller

final ProfileController _profileController = Get.put(ProfileController(), tag: "12345etc");

i will use that tag inside ProfileController but i can not access that tag.

class ProfileController extends GetxController {
    //load info from 12345etc userid
    Future<void> viewProfile() {
        Services.loadProfileInfo("12345etc")...
    }
}

tag 12345etc will load from another controller it wont be a fixed text.


Solution

  • Use field to keep the tag inside the controller

    class ProfileController extends GetxController {
        final String tag;
    
        ProfileController (this.tag);
    
        Future<void> viewProfile() {
            // And use it like this
            Services.loadProfileInfo(tag);
        }
    }
    

    And pass the tag during instantiation

    Get.put(ProfileController("12345etc"), tag: "12345etc");