Search code examples
flutterflutter-getx

The following message was thrown building GetX<LogoPickerController>(controller: Instance of 'LogoPickerController', tag: null, has builder, dirty


I am using Getx. But i amfacing this issue:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following message was thrown building GetX(controller: Instance of
'LogoPickerController', tag: null, has builder, dirty, state: GetXState#ec9a5(controller: Instance of 'LogoPickerController')): [Get] the improper use of a GetX has been detected. You should only use GetX or Obx for the specific widget that will be updated. If you are seeing this error, you probably did not insert any observable variables into GetX/Obx or insert them outside the scope that GetX considers suitable for an update (example: GetX => HeavyWidget => variableObservable). If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.

The Code Is:

 Center(
            child: GetX<LogoPickerController>(
                init: LogoPickerController(),
                builder: (controller) {
                  if (controller.image?.value != null) {
                    return CircleAvatar(
                      radius: 30,
                      child: ClipOval(
                          child: Image.file(
                        controller.image?.value ?? File(""),
                        width: 200,
                        height: 200,
                        fit: BoxFit.fill,
                      )),
                    );
                  } else {
                    return const EntityLogoPickerWidget();
                  }
                }),
          ),

Solution

  • GetX and Obx are preferred to be used when we are using reactive variables.

    Try replacing GetX with GetBuilder. That is

    Center(
                child: GetBuilder<LogoPickerController>(
                    init: LogoPickerController(),
                    initState: (_) {},  //NOT NEEDED
                    builder: (controller) {
                      if (controller.image?.value != null) {
                        return CircleAvatar(
                          radius: 30,
                          child: ClipOval(
                              child: Image.file(
                            controller.image?.value ?? File(""),
                            width: 200,
                            height: 200,
                            fit: BoxFit.fill,
                          )),
                        );
                      } else {
                        return const EntityLogoPickerWidget();
                      }
                    }),
              ),