0

for eg : there is three controllers in our Fruits App project (just random metaphore)

  1. apple controller (which contains apple object)
  2. mango controller (which contains mango object)
  3. fruits controller(which contains list of fruits [apple, mango])

so, here's the main topic we injected apple & mango controller into fruits controller and store the apple and mango object into fruits list ..

so, what im asking is , Is it good practice of getting things done in getx like this way ?!

can we inject multiple other different controllers deendencies into one controller ?! Is it good practice of getting things done in getx ?! is it anti-pattern ?!

someone pls explain me this riddle cause my mind is conflicted right now thinking that is it the way of getting things done or is it breaking the pattern / rules itself !!

Himmat rai
  • 57
  • 1
  • 6

1 Answers1

0

You can inject other controllers into another controller. But the thing is if you should or not. In this case, you should keep in mind just few things. Not just for Flutter or GetX projects, but for any other projects. Controllers usually are considered to be presentation layer objects in n-tier architectures. They are usually used to control the UI with respect to data. Data usually comes from some kind of repository interfaces. So if it's just data (apple, mango) you should just use and inject repository (fruit repository). And if you want to manipulate UI from other controller, you can find the controller instance of that UI by using final appleController = Get.find<AppleController>(); instead of directly injecting it to the constructor. Despite you will not face any problems when using constructor injection of controllers most of the time, I have faced some issues in some very rare cases. Like, if the ApplePage never visited before visiting the FruitPage or when there are multiple ways (routes) to get to the page. Because unlike repositories (which are simple Dart objects with no lifecycles associated), controllers have managed lifecycles. Therefore I would recommend controller dependencies to be managed solely by the GetX dependency management system.

You can use the GetX binding interface and use Get.lazyPut(()=>AppleController())

S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30