Search code examples
flutterflutter-getx

Flutter GetX Obx Rebuild After Controller has beed deleted


I'm new to flutter and I'm using GetX.

My widget:

Obx(() => Radio(
              value: Get.find<BuyController>().paymentRadioList[index],
              groupValue:
                  Get.find<BuyController>().paymentRadioGroupValue.value,
              fillColor: MaterialStateProperty.all(theme.colorScheme.primary),
              onChanged: (value) {
                if (value == null) {
                  return;
                }
                Get.find<BuyController>().paymentRadioGroupValue.value = value;
              }))

And BuyController is initiated just here:

@override
  void initState() {
    super.initState();
    buyController = Get.put(BuyController());
  }

Once the user done the payment, I will call Get.back(); to pop this page and go to the home page.

But I feel there is a problem that, due to the Obx widget is using Get.find(), once the page is popped, the controller is deleted and obx will try to rebuild the widget, causing LateInitializationError: Field 'vDf' has not been initialized.

Does anyone have any solution for this circumstance? Thank you.

Once the page was popped, obx shouldn't try to rebuild


Solution

  • I think this will do the job.

    • Get.put(Controller(), permanent: true);