getx icon indicating copy to clipboard operation
getx copied to clipboard

Null controller with GetWidget + Get.Create when access controller in GetWidget constructor

Open ynixt opened this issue 3 years ago • 1 comments

Describe the bug

Using GetWidget together with Get.Create generates a null error for the controller if we tries to access the controller inside the GetWidget constructor. Note that access the controller via the constructor is allowed with GetView

**Reproduction code

widget:

class GridVerticalIconButton extends GetWidget<GridVerticalIconButtonController> {
  GridVerticalIconButton(
      {String title,
      Key? key})
      : super(key: key) {
    controller.title = title; // Null error here
  }
}

controller:

class GridVerticalIconButtonController extends GetxController {
  late String title;
}

bindings:

class GlobalBindings implements Bindings {
  @override
  void dependencies() {
    Get.create(() => GridVerticalIconButtonController());
  }
}

To Reproduce Steps to reproduce the behavior:

Expected behavior Should be able to access controller in the GetWidget constructor

Flutter Version: 2.5.3

Getx Version: 4.6.1

Describe on which device you found the bug: Samsung Galaxy S21+

Minimal reproduce code Try to access the controller in a widget constructor that extends GetWidget

ynixt avatar Dec 16 '21 05:12 ynixt

This is interesting, because the only solution would be calling Get.find beforehand and that's horrible

RafaRuiz avatar Aug 31 '22 14:08 RafaRuiz