3

I am just looking forward to understanding the difference between using a normal setState(() {}) and the update() method of the Getx package.

As much as I can see from the practical point of view when setState(() {]) is used the whole page is rebuilt but when GetX has used only the part of .obs is rebuilt. What I want to understand is more in-depth difference.

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • check [List of state management approaches](https://docs.flutter.dev/development/data-and-backend/state-mgmt/options) – pskink Jun 01 '22 at 05:03

2 Answers2

3

setState will refresh the whole widget. But using the Getx you can refresh the particular widget.

More you can find from this link : Getx Controller

Jay K
  • 56
  • 2
1

Getx's state manager, is a tool that let you control and manage your widget state from a separate place, which is the GetxController.

The Getx state management widgets, like GetBuilder(), Getx(), Obx()... are StatfulWidgets and under the hood, they also use a normal SetState(() {}), but the implementation of calling them is really different than a usual setState(() {}) call.

As example, the GetBuilder() :

class GetBuilder<T extends GetxController> extends StatefulWidget {
  final GetControllerBuilder<T> builder;
  final bool global;
  final Object? id;
  final String? tag;
  final bool autoRemove;
  final bool assignId;
  final Object Function(T value)? 
  /*...*/

and under the hood, it updates the state by getting a method like this :

  void getUpdate() {
    if (mounted) setState(() {});
  }

then store it in Map we call it from the controller with an update().


Your sentence:

As much as I can see from a practical point of view when setState is used the whole page is rebuilt but when GetX has used only the part of .obs is rebuilt

is wrong!

Try wrapping your whole page with a GetBuilder and call the update() from its controller, and you will have a full rebuild for the whole page because it's just a normal StatefulWidget that will be rebuilt by calling its build() method again and again...

You face the whole page state update because you wrap the whole of it with a StatfulWidget, the same thing with GetBuilder(), Obx()...

There is a Flutter builder widget that let you also manage the state of its child just locally, which is StatefulBuilder, give it a quick check and I recommend that you play with it to understand that approach of using a builder widget to update the state.

Besides that Getx gives you the ability to separate your logic and state management into the GetxController, it let you control which widget to update exactly with its own custom mechanisms like using the update() method ( which calls a normal SetState(() {}) ) with a specific id, like this:

update([id1, id2, id3]);

Under the hood search over a Map where it stores all the SetState(() {}) that it got from the Getx's GetBuilder(), then look for what matches that id, then call only what belongs to it, and this what causes that widget having one of those id will be updated, and other widgets will not.

you can check more about how Getx works with a quick reads of its source code, I guess it will be very helpful for you.

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35