GetX controllers with custom tags

Udit Swaroopa
1 min readNov 7, 2022

The objective of this article is to create controllers with custom tags and access the controllers with tags in the view

Firstly we will declare a constant tag name in our constants

const onboardScreenTag = ‘authentication-controller’;

Now, when initialising the controllers using bindings we will pass the tag name

Get.offAll(
() => const OnboardLoginScreen(),
binding: OnboardScreenBindings(tag: onboardScreenTag)
);

this will initialise the controllers with the tag name we have passed

class OnboardScreenBindings implements Bindings {String tag;OnboardScreenBindings({required this.tag});@override
void dependencies() {
//controller with custom tag name
Get.lazyPut(() => AuthenticationController(), tag: tag);
}
}

So finally, we have a controller with custom tag name but we are not yet done because in the view when we will try to get find controller it will throw an error because by default Getx search controller with tag name as null

So we need to overwrite the tag name in view which can be done using the setter method

final String screenTag;@overrideString? get tag => screenTag; //overwriting null tag nameconst OnboardLoginScreen({super.key, required this.screenTag});

Now, because whenever we need to navigate to the OnboardLoginScreen we need to also pass the same tag name with which the controller was initialised this can be done when we are navigating the user

Get.offAll(
() => const OnboardLoginScreen(screenTag: onboardScreenTag),
binding: OnboardScreenBindings(tag: onboardScreenTag),
);

So finally, we have a controller with custom tag name that can be accessed in the view!

--

--

Udit Swaroopa
0 Followers

In love with designing, but in a relationship with Technology! 😅