Reputation: 29
where should I use GetBuilder, GetX, or Obx in a flutter? I saw some answers on the internet. but can you share a simple explanation? which one should I use when I want to read data from the firebase collection?
Upvotes: 2
Views: 12632
Reputation: 6127
Obx and GetX use streams, but GetBuilder does not. Hence, GetBuilder consumes much less RAM.
GetBuilder should be your default choice. Use it whenever you can.
The controller for GetBuilder should be created this way:
class Controller extends GetxController {
int counter = 0;
void increment() {
counter++;
update();
}
}
Obx or GetX should be used only when required. For example, you work with Firebase and you want to write something like this:
class TodoController extends GetxController {
RxList<Todo> todoState = <Todo>[].obs;
void addTodo (Todo todo){
todoState.bindStream(todoService.getTodoStream());
}
}
Obx and GetX are almost the same. Obx according to official docs is slightly more performant than GetX. Using GetX we can specify and init controller, but in Obx we cannot. This is not a big advantage IMHO, since, the controller can be easily initialized elsewhere. But inside Obx we can use several controllers.
I don't see a good use case for GetX and always use Obx. More info in this article
Upvotes: 2
Reputation: 11
use GetBuilder
when you want to update the state of a widget manually from your controller, with update()
, use Obx
, when you want to update a widget based on the value of an observable variable .
Upvotes: 1
Reputation: 1231
You can use GetBuilder, GetX, or Obx in different situations during Flutter app development:
GetBuilder: You can use GetBuilder when you want to update only a small portion of your widget's view based on specific state changes. For example, if you have multiple states managed within a controller and only want to update a few widgets that depend on those states, you can use GetBuilder to build those specific widgets.
GetX: GetX combines the features of GetBuilder, Obx, and other functionalities provided by GetX. You can use GetX when you want to observe state changes and automatically update the widgets that depend on them. GetX performs better as it only updates the necessary widgets.
Obx: Obx stands for "Observer Widget". You can use Obx when you want to update all the widgets associated with the observed state. Obx will automatically rebuild the widget whenever there is a change in the observed state. This is useful when you have multiple widgets depending on several states and you want to ensure that all those widgets are always updated according to the state changes.
The choice between GetBuilder, GetX, or Obx depends on your needs and the complexity of your application. If you only need to update a small portion of your widgets, GetBuilder can be a good choice. If you want to automatically update widgets based on state changes, GetX or Obx can be better options.
Upvotes: 5
Reputation: 9196
Simply:
use GetBuilder
when you want to update the state of a widget manually from your controller, with update()
,
use Obx
, when you want to update a widget based on the value of an observable variable .obs
, so whenever you change its value, the Obx
will update automatically in your app.
use Getx
when you want to update a specific Widget with an id as an example automatically, you can think of it like GetBuilder
but with Obx observation
but consider using them carefully, because based on the docs, the Getbuilder consumes fewer resources so it has better performance, and Obx
is based on streams so it consumes more resources.
there is more to learn about them from the official docs
Upvotes: 13