Flutter and getx: update() does not notify GetBuilder

Issue

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

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().

Answered By – asored

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.