'Flutter and getx: update() does not notify GetBuilder

I have a very strange issue for my current project using Flutter and getx package. Short: the Notifier is not working. On initState from a GetBuilder I try to update an variable (.obs) and call update(), but the view knows nothing about it.

Reproduction code

GetxController:

class HoursController extends GetxController {
  final AwController awController = AwController();
  final hours = [].obs;

  void loadHours({required Kunde kunde}) async {
    this.hours.assignAll(["Test1", "Test2"]);
    update(["hours"]);
  }
}

View:

GetBuilder<HoursController>(
  id: "hours",
  initState: (_) {
    HoursController().loadHours(kunde: this.kunde);
  },
  builder: (controller) {
    if (controller.hours.length == 0) {
      return Container(
        child: Text("Nothing"),
      );
    }
    return ListView.builder(
        shrinkWrap: true,
        physics: ClampingScrollPhysics(),
        itemCount: controller.hours.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text(controller.hours[index]),
          );
        });
  },
)

Expected behavior After calling the function on initState, which changes the hours variable, I expect that the update["hours"] will notify the view to refresh the state. But nothing happens. The console shows the correct data. So the variable really was refreshed, but the view was not notified about it.

Flutter Version: Flutter 2.2.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision f4abaa0735 (8 weeks ago) • 2021-07-01 12:46:11 -0700 Engine • revision 241c87ad80 Tools • Dart 2.13.4

Getx Version: 4.3.8

By the way I'm working with Flutter Desktop



Solution 1:[1]

Ok, solved... the issue was the HoursController().loadHours(kunde: this.kunde);. Here I create a new instance of HoursController. But I need to use the instance created above with Get.find().

Solution 2:[2]

if your GetBuilder is not updating with update() method then also check if you're overriding refresh method of your controller without calling super.refresh() in the overridden method. I had the same problem took me awhile to realise.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 asored
Solution 2 mumboFromAvnotaklu