GetX in Flutter
Introduction
GetX is a flutter package. This package helps us to reduce the usage of stateful widgets and also separating the business logic from the UI. It has been nearly 4 months since I started using this. And personally I’ve enjoyed working with this package. It helps you to mimic MVC folder structure in your product.
GetBuilder, GetX and Obx are builders provided by GetX package. These helps to update or rebuild the listeners who are listening to the change of a particular controller.
There are few different confusions which I faced while using the GetBuilder, GetX and Obx. So later I experimented with each of them. And writing those results and my conclusions.
Experiment Cases
GetBuilder
(with update())
Case 1a: Without .obs
Updating
Case 1b: With .obs
updating
(without update())
Case 2a: With .obs
not updating
Case 2b: Without .obs
not updating
GetX
(with update())
Case 1a: Without .obs
Error.
Case 1b: With .obs
updating
(without update())
Case 2a: With .obs
Updating
Case 2b: Without .obs
Error.
Error:
the improper use of a GetX has been detected.
You should only use GetX or Obx for the specific widget that will be updated.
Obx
(with update())
Case 1a: Without .obs
Error.
Case 1b: With .obs
updating
(without update())
Case 2a: With .obs
Updating
Case 2b: Without .obs
Error.
Error:
the improper use of a GetX has been detected.
You should only use GetX or Obx for the specific widget that will be updated.
Conclusions
When GetBuilder is used, it doesn’t matter whether you are using reactive variables or not. It only listens to update() call. But both GetX and Obx are reactive. And the observable variables notify, GetX and Obx, when they change their value.
So you must use .obs variables to listen in Obx or GetX and don’t need update().