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

Issue

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

Solution

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

Answered By – Baker

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.