flutter get when to use GetX<Controller>, GetBuilder<Controller> or Obx()

11.5k Views Asked by At

As the title reads, there are a couple of ways to update state. When should I choose one over another?

1

There are 1 best solutions below

0
On BEST ANSWER

There's no hard rules about this, but here's how I try to decide:

Obx

  • when my controller is already registered elsewhere and I want
  • minimal code/noise
  • a reactive widget

GetX

  • when my controller isn't yet registered or
  • I want to be very explicit/obvious which controller is being used or
  • I need to run some initState calls during creation and I want
  • a reactive widget

GetBuilder

  • I want to manually decide when a widget rebuilds
  • I have several state variables that make sense to refresh together as a group

Notes

Under the hood, both Obx and GetX use streams, subscribing to controller observable variables change streams to know when to reactively rebuild.

GetBuilder does not.

GetX and GetBuilder both extend StatefulWidget