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
this is my first controller
class firstController extends GetxController { var id = 1; }
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!
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());
}