Unhandled Exception: Stack Overflow , Flutter GetXController is being initialized for 2000 times

Issue

I’m putting my GetXController only once on the home page using :

class NewOrdersList extends StatelessWidget {

  final OrdersController ordersC = Get.put(OrdersController());
...

then whenever I need the controller I simply use

  OrdersController ordersC = Get.find();

and the controller itself:

  @override
  void onInit() {
    trackLocation();
    super.onInit();
  }

at the end of the function trackLocation(); I use OrdersController ordersC = Get.find(); because I need to access a variable inside the controller.

but when u launch the application I see that the controller has been initialized thousands of times.

enter image description here

Solution

You don’t need to write Get.find() everytime if you are using controller in same class. If you need to access your OrderController's instance in some other class, then and only then you can use Get.find. And don’t write OrderController ordersC = Get.find() after trackLocation(). Because once you initialize your controller (Either put or find) , then you can access all variables from it. Never initialize your controller multiple time in your code.

Answered By – Vaidarbhi

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *