Getx: GetController or GetxController?

Created on 4 Jul 2020  路  7Comments  路  Source: jonataslaw/getx

I am confused, the document refer all to GetxController, but the example code using GetController.
What is the different between these 2? Which one should we use?

Most helpful comment

There are several ways to do this.

The first:

Controller controller = Controller(); // without Get.put()

The second, if you are injecting dependency directly on the widget:

GetBuilder<Controller>( // or GetX
init: Controller(),
global: false, // remembering that this will make the controller available only within GetBuilder
builder: (controller) => Text(controller.name)
[...]

The third is using tags:

Controller c1 = Get.put(Controller(), tag: 'ct1');
Controller c2 = Get.put(Controller(), tag: 'ct2');
Controller c3 = Get.put(Controller(), tag: 'ct3');

There is still a longer way using abstractions and override: true, but it is not documented because I am not sure if I will keep the api, so I will not explain about it here.

All 7 comments

The sample code for the repository is from version 2.0 that still used Get controller.
In version 3 you must use GetxController

@jonataslaw is it possible to have more than one instance of the same type controller?

Example:
In a browser the state controller will be the same kind for every page you open.

@peekpt you don't need multiple instances of your controller

you can use the same single instance in the entire app, using dependency injection
and what do you mean the same state controller for every page? you mean that in all pages you gonna use the exact same data displaying the exact same thing?

or you gonna use the controller to show different data in every page? if that's the case, use one controller per page

@jonataslaw yes I'm consider a controller of the same type.
Example: A Bluetooth app that will have unknown number connected devices on unknown number of tabs, each tab independently uses the same type of controller to display the live data. How can I do a find to the correct controller if they are the same type, is there any find by id

well, i did not understand yet but i am deviating from the subject.
As far as i know, it is not possible to do that
@jonataslaw can you confirm if what he asked is possible on get?

There are several ways to do this.

The first:

Controller controller = Controller(); // without Get.put()

The second, if you are injecting dependency directly on the widget:

GetBuilder<Controller>( // or GetX
init: Controller(),
global: false, // remembering that this will make the controller available only within GetBuilder
builder: (controller) => Text(controller.name)
[...]

The third is using tags:

Controller c1 = Get.put(Controller(), tag: 'ct1');
Controller c2 = Get.put(Controller(), tag: 'ct2');
Controller c3 = Get.put(Controller(), tag: 'ct3');

There is still a longer way using abstractions and override: true, but it is not documented because I am not sure if I will keep the api, so I will not explain about it here.

Example updated. Closing this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jemariads picture jemariads  路  4Comments

Denilson-source picture Denilson-source  路  3Comments

GoldenSoju picture GoldenSoju  路  3Comments

R-Praveen picture R-Praveen  路  4Comments

definev picture definev  路  3Comments