9

I'm new in Flutter and so with GetX https://pub.dev/packages/get

Am I able to access value from "another controller" inside a controller?

Both of them will be initialized but I don't want to pass "id" at screens/widget/handlers etc., I want to do it between controller to controller only (if possible)

Here's an example

  1. this is my first controller

    class firstController extends GetxController { var id = 1; }

  2. this is my second controller and I want to access id from firstController

    class secondController extends GetxController { var copiedIdFromFirstController = 1; }

I know this sounds silly but I love exploring things (lol)

Thank you in advance!

Koala
  • 352
  • 2
  • 7
  • 18

2 Answers2

15

If you mean accessing one controller directly from another controller class, then yes you can.

class FirstController extends GetxController {
  int id = 1;
}

class SecondController extends GetxController {
  int idFromFirstController = Get.find<FirstController>().id;

  @override
    void onInit() {
      super.onInit();
      debugPrint('$idFromFirstController'); // prints 1
    }
}

The only thing you need to make sure is that the dependency is initialized first. So one way to make the above example work is to initialize both in main.

void main() {
  Get.put(FirstController()); // make sure this is first
  Get.put(SecondController());
  runApp(MyApp());
}
Loren.A
  • 4,872
  • 1
  • 10
  • 17
  • Hello there, This is exactly what I want to do, Thank you! – Koala Apr 23 '21 at 08:17
  • Anything should I concern about this approach? Is it recommended or warned anywhere in the documentation? – sarunw May 12 '21 at 08:10
  • I suppose that dives more into a general architecture question. But the GetX docs have examples of controllers depending on other controllers so I would't worry about it. Probably a good idea to have some core controllers that don't depending on anything else though, like database and API calls etc... – Loren.A May 12 '21 at 14:25
  • How would you listen to the first controller's ID variable so the second controller reacts to changes to the ID variable? – fractal Jun 12 '21 at 15:59
  • @fractal make `id` observable by adding `.obs` then subscribe to it using `ever` function. You can see an explanation of `ever` on this post here https://stackoverflow.com/questions/67134177/proper-way-to-persist-store-data-in-getxcontroller-in-flutter/67292928#67292928 – Loren.A Jun 23 '21 at 17:47
  • @Loren.A could you provide a full example of this? Sadly I don't get it working.. – Jan Mar 28 '22 at 13:07
  • @Jan that pretty much is a full working example. If you're having issues then post a separate question and paste the link here and I'll have a look. – Loren.A Mar 28 '22 at 14:31
  • @Loren.A thank you for your answer. I already found my problem! – Jan Mar 29 '22 at 15:11
  • Get.put(ControllerName(), permanent: true); This will keep instance in memory so you can communicate between each controller – RAMU PAL Jul 06 '23 at 11:57
3

Yes you can do that like this:

class yourWidget extends StatelessWidget{
     FirstController fCTRL = Get.put(FirstController());
     SecondController sCTRL = Get.put(SecondController());
     //now you can have 
     print(fCTRL.id);  //1
     print(sCTRL.something);
}
Kaival Patel
  • 461
  • 3
  • 8