Nipodemos

change the way of speaking things to be less "it's magic!" and more serious in state management

1 -  
2 # State Management 1 # State Management
3 2
4 There are currently several state managers for Flutter. However, most of them involve using ChangeNotifier to update widgets and this is a bad and very bad approach to performance of medium or large applications. You can check in the official Flutter documentation that [ChangeNotifier should be used with 1 or a maximum of 2 listeners](https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html), making it practically unusable for any application medium or large. 3 There are currently several state managers for Flutter. However, most of them involve using ChangeNotifier to update widgets and this is a bad and very bad approach to performance of medium or large applications. You can check in the official Flutter documentation that [ChangeNotifier should be used with 1 or a maximum of 2 listeners](https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html), making it practically unusable for any application medium or large.
@@ -11,532 +10,560 @@ Other state managers are good, but have their nuances: @@ -11,532 +10,560 @@ Other state managers are good, but have their nuances:
11 10
12 Get isn't better or worse than any other state manager, but that you should analyze these points as well as the points below to choose between using Get in pure form (Vanilla), or using it in conjunction with another state manager. Definitely, Get is not the enemy of any other state manager, because Get is a microframework, not just a state manager, and can be used either alone or in conjunction with them. 11 Get isn't better or worse than any other state manager, but that you should analyze these points as well as the points below to choose between using Get in pure form (Vanilla), or using it in conjunction with another state manager. Definitely, Get is not the enemy of any other state manager, because Get is a microframework, not just a state manager, and can be used either alone or in conjunction with them.
13 12
14 -## Simple State Manager 13 +## Reactive State Manager
15 14
16 -Get has a state manager that is extremely light and easy, which does not use ChangeNotifier, will meet the need especially for those new to Flutter, and will not cause problems for large applications. 15 +Reactive programming can alienate many people because it is said to be complicated. GetX turns reactive programming into something quite simple:
17 16
18 -### Advantages 17 +- You won't need to create StreamControllers.
  18 +- You won't need to create a StreamBuilder for each variable
  19 +- You will not need to create a class for each state.
  20 +- You will not need to create a get for an initial value.
19 21
20 -1. Update only the required widgets. 22 +Reactive programming with Get is as easy as using setState.
21 23
22 -2. Does not use changeNotifier, it is the state manager that uses less memory (close to 0mb). 24 +Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed.
23 25
24 -3. Forget StatefulWidget! With Get you will never need it. With the other state managers, you will probably have to use a StatefulWidget to get the instance of your Provider, BLoC, MobX Controller, etc. But have you ever stopped to think that your appBar, your scaffold, and most of the widgets that are in your class are stateless? So why save the state of an entire class, if you can only save the state of the Widget that is stateful? Get solves that, too. Create a Stateless class, make everything stateless. If you need to update a single component, wrap it with GetBuilder, and its state will be maintained. 26 +This is your count variable:
25 27
26 -4. Organize your project for real! Controllers must not be in your UI, place your TextEditController, or any controller you use within your Controller class. 28 +```dart
  29 +var name = 'Jonatas Borges';
  30 +```
27 31
28 -5. Do you need to trigger an event to update a widget as soon as it is rendered? GetBuilder has the property "initState", just like StatefulWidget, and you can call events from your controller, directly from it, no more events being placed in your initState. 32 +To make it observable, you just need to add ".obs" to the end of it:
29 33
30 -6. Do you need to trigger an action like closing streams, timers and etc? GetBuilder also has the dispose property, where you can call events as soon as that widget is destroyed. 34 +```dart
  35 +var name = 'Jonatas Borges'.obs;
  36 +```
31 37
32 -7. Use streams only if necessary. You can use your StreamControllers inside your controller normally, and use StreamBuilder also normally, but remember, a stream reasonably consumes memory, reactive programming is beautiful, but you shouldn't abuse it. 30 streams open simultaneously can be worse than changeNotifier (and changeNotifier is very bad). 38 +That's all. It's *that* simple.
33 39
34 -8. Update widgets without spending ram for that. Get stores only the GetBuilder creator ID, and updates that GetBuilder when necessary. The memory consumption of the get ID storage in memory is very low even for thousands of GetBuilders. When you create a new GetBuilder, you are actually sharing the state of GetBuilder that has a creator ID. A new state is not created for each GetBuilder, which saves A LOT OF ram for large applications. Basically your application will be entirely Stateless, and the few Widgets that will be Stateful (within GetBuilder) will have a single state, and therefore updating one will update them all. The state is just one. 40 +What did we do under the hood? We created a stream of Strings, assigned the initial value "Jonatas Borges", we warn all widgets that use "Jonatas Borges" that they now belong to this variable, and when it is changed, they will be changed too. This is the magic of Get, that only dart allows us to do this.
35 41
36 -9. Get is omniscient and in most cases it knows exactly the time to take a controller out of memory. You should not worry about when to dispose of a controller, Get knows the best time to do this. 42 +Okay, but as we know, a widget can only be changed if it is inside a function, because static classes do not have the power to "auto-change". You will need to create a StreamBuilder, subscribe to listen to this variable, and create a "cascade" of StreamBuilder if you want to change several variables in the same scope, right?
  43 +No, you don't need a StreamBuilder, but you are right about static classes.
37 44
38 -### Usage 45 +Well, in the view we usually have a lot of boilerplate when we want to change a specific widget. With Get you can also forget about this Boilerplate. StreamBuilder? initialValue? builder?
  46 +No, you just need to play this variable inside an Obx widget.
39 47
40 ```dart 48 ```dart
41 -// Create controller class and extends GetxController  
42 -class Controller extends GetxController {  
43 - int counter = 0;  
44 - void increment() {  
45 - counter++;  
46 - update(); // use update() to update counter variable on UI when increment be called  
47 - }  
48 -}  
49 -// On your Stateless/Stateful class, use GetBuilder to update Text when increment be called  
50 -GetBuilder<Controller>(  
51 - init: Controller(), // INIT IT ONLY THE FIRST TIME  
52 - builder: (_) => Text(  
53 - '${_.counter}',  
54 - ),  
55 -)  
56 -//Initialize your controller only the first time. The second time you are using ReBuilder for the same controller, do not use it again. Your controller will be automatically removed from memory as soon as the widget that marked it as 'init' is deployed. You don't have to worry about that, Get will do it automatically, just make sure you don't start the same controller twice. 49 +Obx (() => Text (controller.name));
57 ``` 50 ```
58 51
59 -**Done!** 52 +What do you need to memorize? "Obx(() =>" You are just passing that widget through an arrow-function into an Obx. Obx is smart, and will only be changed if the value of name is changed. If name is "John" and you change it to "John", it will not have any changes on the screen, and Obx will simply ignore this change, and will not rebuild the widget, to save resources. Isn't that amazing?
60 53
61 -- You have already learned how to manage states with Get. 54 +What if I have 5 observable variables within an Obx? It will update when any of them are changed. And if I have 30 variables in a class, when I update one, will it update all variables that are in that class? No, just the specific widget that uses that variable.
62 55
63 -- Note: You may want a larger organization, and not use the init property. For that, you can create a class and extends Bindings class, and within it mention the controllers that will be created within that route. Controllers will not be created at that time, on the contrary, this is just a statement, so that the first time you use a Controller, Get will know where to look. Get will remain lazyLoad, and will continue to dispose Controllers when they are no longer needed. See the pub.dev example to see how it works. 56 +GetX only updates the screen when the variable changes on the screen, if the screen remains the same, it will not reconstruct anything.
64 57
65 -If you navigate many routes and need data that was in your previously used controller, you just need to use GetBuilder Again (with no init): 58 +### Advantages
66 59
67 -```dart  
68 -class OtherClass extends StatelessWidget {  
69 - @override  
70 - Widget build(BuildContext context) {  
71 - return Scaffold(  
72 - body: Center(  
73 - child: GetBuilder<Controller>(  
74 - builder: (s) => Text('${s.counter}'),  
75 - ),  
76 - ),  
77 - );  
78 - } 60 +GetX is focused for when you need granular control over what is being updated. Is for situations where you want only the widget when a certain variable has been changed to be rebuilt.
79 61
80 -``` 62 +If you do not need unique IDs, because all your variables will be changed when you perform an action, use GetBuilder, because it is a simple state updater in blocks, made in a few lines of code. It was done in a simple way, to have the least computational logic involved, just to fulfill a single purpose and spend the minimum resources possible for that purpose.
81 63
82 -If you need to use your controller in many other places, and outside of GetBuilder, just create a get in your controller and have it easily. (or use `Get.find<Controller>()`) 64 +If you want a powerful state manager, you can go without fear to GetX. It does not work with variables, but flows, everything in it is streams under the hood. You can use rxDart in conjunction with it, because everything is stream, you can hear the event of each "variable", because everything in it is stream, it is literally BLoC, easier than MobX, and without code generator or decorations .
83 65
84 -```dart  
85 -class Controller extends GetxController { 66 +Without decorations, you can turn anything into Observable with just a ".obs".
86 67
87 - /// You do not need that. I recommend using it just for ease of syntax.  
88 - /// with static method: Controller.to.counter();  
89 - /// with no static method: Get.find<Controller>().counter();  
90 - /// There is no difference in performance, nor any side effect of using either syntax. Only one does not need the type, and the other the IDE will autocomplete it.  
91 - static Controller get to => Get.find(); // add this line 68 +Maximum performance: In addition to having a smart algorithm for minimal reconstruction, Get uses comparators to make sure the state has changed. If you experience any errors in your application, and send a duplicate change of state, Get will ensure that your application does not collapse.
92 69
93 - int counter = 0;  
94 - void increment() {  
95 - counter++;  
96 - update();  
97 - }  
98 -}  
99 -``` 70 +The state only changes if the values ​​change. That's the main difference between Get, and using Computed from MobX for example. When joining two observables, when one is changed, the hearing of that observable will change. With Get, if you join two variables (which is unnecessary computed for that), GetX (similar to Observer) will only change if it implies a real change of state.
100 71
101 -And then you can access your controller directly, that way: 72 +### Usage
  73 +
  74 +You have 3 ways to turn a variable into an observable.
  75 +The first is using Rx{Type}.
102 76
103 ```dart 77 ```dart
104 -FloatingActionButton(  
105 - onPressed: () {  
106 - Controller.to.increment(),  
107 - } // This is incredibly simple!  
108 - child: Text("${Controller.to.counter}"),  
109 -), 78 +var count = RxString();
110 ``` 79 ```
111 80
112 -When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. 81 +The second is to use Rx and type it with `Rx<Type>`
113 82
114 -### How it handles controllers 83 +```dart
  84 +var count = Rx<String>();
  85 +```
115 86
116 -Let's say we have this: 87 +The third, more practical and easier approach, is just to add an .obs to your variable.
117 88
118 -`Class a => Class B (has controller X) => Class C (has controller X)` 89 +```dart
  90 +var count = 0.obs;
119 91
120 -In class A the controller is not yet in memory, because you have not used it yet (Get is lazyLoad). In class B you used the controller, and it entered memory. In class C you used the same controller as in class B, Get will share the state of controller B with controller C, and the same controller is still in memory. If you close screen C and screen B, Get will automatically take controller X out of memory and free up resources, because Class a is not using the controller. If you navigate to B again, controller X will enter memory again, if instead of going to class C, you return to class A again, Get will take the controller out of memory in the same way. If class C didn't use the controller, and you took class B out of memory, no class would be using controller X and likewise it would be disposed of. The only exception that can mess with Get, is if you remove B from the route unexpectedly, and try to use the controller in C. In this case, the creator ID of the controller that was in B was deleted, and Get was programmed to remove it from memory every controller that has no creator ID. If you intend to do this, add the "autoRemove: false" flag to class B's GetBuilder and use adoptID = true; in class C's GetBuilder. 92 +// or Rxint count = 0.obs;
  93 +// or Rx<int> count = 0.obs;
  94 +```
121 95
122 -### You won't need StatefulWidgets anymore 96 +As we know, Dart is now heading towards null safety. Since that it is a good idea, from now on, you should start to use your variables always with an initial value. Transforming a variable into an observable with an initial value with Get is the simplest and most practical approach. You will literally add a ".obs" to the end of your variable, and that’s it, you’ve made it observable, and its value will be the initial value.
123 97
124 -Using StatefulWidgets means storing the state of entire screens unnecessarily, even because if you need to minimally rebuild a widget, you will embed it in a Consumer/Observer/BlocProvider/GetBuilder/GetX/Obx, which will be another StatefulWidget.  
125 -The StatefulWidget class is a class larger than StatelessWidget, which will allocate more RAM, and this may not make a significant difference between one or two classes, but it will most certainly do when you have 100 of them!  
126 -Unless you need to use a mixin, like TickerProviderStateMixin, it will be totally unnecessary to use a StatefulWidget with Get. 98 +You can add variables, and if you want to type your widget to get your controller inside, you just need to use GetX widget instead of Obx
127 99
128 -You can call all methods of a StatefulWidget directly from a GetBuilder.  
129 -If you need to call initState() or dispose() method for example, you can call them directly; 100 +### Example
130 101
131 ```dart 102 ```dart
132 -GetBuilder<Controller>(  
133 - initState: (_) => Controller.to.fetchApi(),  
134 - dispose: (_) => Controller.to.closeStreams(),  
135 - builder: (s) => Text('${s.username}'),  
136 -), 103 +final count1 = 0.obs;
  104 +final count2 = 0.obs;
  105 +int get sum => count1.value + count2.value;
137 ``` 106 ```
138 107
139 -A much better approach than this is to use the onInit() and onClose() method directly from your controller.  
140 -  
141 ```dart 108 ```dart
142 -@override  
143 -void onInit() {  
144 - fetchApi();  
145 - super.onInit();  
146 -} 109 +GetX<Controller>(
  110 + builder: (value) {
  111 + print("count 1 rebuild");
  112 + return Text('${value.count1.value}');
  113 + },
  114 +),
  115 +GetX<Controller>(
  116 + builder: (_) {
  117 + print("count 2 rebuild");
  118 + return Text('${_.count2.value}');
  119 + },
  120 +),
  121 +GetX<Controller>(
  122 + builder: (_) {
  123 + print("count 3 rebuild");
  124 + return Text('${_.sum}');
  125 + },
  126 +),
147 ``` 127 ```
148 128
149 -- NOTE: If you want to start a method at the moment the controller is called for the first time, you DON'T NEED to use constructors for this, in fact, using a performance-oriented package like Get, this borders on bad practice, because it deviates from the logic in which the controllers are created or allocated (if you create an instance of this controller, the constructor will be called immediately, you will be populating a controller before it is even used, you are allocating memory without it being in use, this definitely hurts the principles of this library). The onInit() methods; and onClose(); were created for this, they will be called when the Controller is created, or used for the first time, depending on whether you are using Get.lazyPut or not. If you want, for example, to make a call to your API to populate data, you can forget about the old-fashioned method of initState/dispose, just start your call to the api in onInit, and if you need to execute any command like closing streams, use the onClose() for that. 129 +If we increment the number of count 1, only count 1 and count 3 are reconstructed, because count 1 now has a value of 1, and 1 + 0 = 1, changing the sum value.
150 130
151 -### Why it exists 131 +If we change count 2, only count2 and 3 are reconstructed, because the value of 2 has changed, and the result of the sum is now 2.
152 132
153 -The purpose of this package is precisely to give you a complete solution for navigation of routes, management of dependencies and states, using the least possible dependencies, with a high degree of decoupling. Get engages all high and low level Flutter APIs within itself, to ensure that you work with the least possible coupling. We centralize everything in a single package, to ensure that you don't have any kind of coupling in your project. That way, you can put only widgets in your view, and leave the part of your team that works with the business logic free, to work with the business logic without depending on any element of the View. This provides a much cleaner working environment, so that part of your team works only with widgets, without worrying about sending data to your controller, and part of your team works only with the business logic in its breadth, without depending on no element of the view. 133 +If we add the number 1 to count 1, which already contains 1, no widget is reconstructed. If we add a value of 1 for count 1 and a value of 2 for count 2, only 2 and 3 will be reconstructed, simply because GetX not only changes what is necessary, it avoids duplicating events.
154 134
155 -So to simplify this:  
156 -You don't need to call methods in initState and send them by parameter to your controller, nor use your controller constructor for that, you have the onInit() method that is called at the right time for you to start your services.  
157 -You do not need to call the device, you have the onClose() method that will be called at the exact moment when your controller is no longer needed and will be removed from memory. That way, leave views for widgets only, refrain from any kind of business logic from it. 135 +- NOTE: By default, the first event will allow rebuild even if it is the same. We created this behavior due to dualistic variables, such as Boolean.
  136 +Imagine you did this:
158 137
159 -Do not call a dispose method inside GetxController, it will not do anything, remember that the controller is not a Widget, you should not "dispose" it, and it will be automatically and intelligently removed from memory by Get. If you used any stream on it and want to close it, just insert it into the close method. Example: 138 +```dart
  139 +var isLogged = false.obs;
  140 +```
  141 +
  142 +and then you check if a user is logged in to trigger an event in "ever".
160 143
161 ```dart 144 ```dart
162 -class Controller extends GetxController {  
163 - StreamController<User> user = StreamController<User>();  
164 - StreamController<String> name = StreamController<String>(); 145 +onInit(){
  146 + ever(isLogged, fireRoute);
  147 + isLogged.value = await Preferences.hasToken();
  148 +}
165 149
166 - /// close stream = onClose method, not dispose.  
167 - @override  
168 - void onClose() {  
169 - user.close();  
170 - name.close();  
171 - super.onClose(); 150 +fireRoute(logged) {
  151 + if (logged) {
  152 + Get.off(Home());
  153 + } else {
  154 + Get.off(Login());
172 } 155 }
173 } 156 }
174 ``` 157 ```
175 158
176 -Controller life cycle:  
177 -  
178 -- onInit() where it is created.  
179 -- onClose() where it is closed to make any changes in preparation for the delete method  
180 -- deleted: you do not have access to this API because it is literally removing the controller from memory. It is literally deleted, without leaving any trace. 159 +if hasToken was false, there would be no change to isLogged, so ever would never be called. To avoid this type of behavior, the first change to an observable will always trigger an event, even if it is the same.
  160 +You can remove this behavior if you want, using:
  161 +`isLogged.firstRebuild = false;`
181 162
182 -### Other ways of using it 163 +### Conditions to rebuild
183 164
184 -You can use Controller instance directly on GetBuilder value: 165 +In addition, Get provides refined state control. You can condition an event (such as adding an object to a list), on a certain condition.
185 166
186 ```dart 167 ```dart
187 -GetBuilder<Controller>(  
188 - init: Controller(),  
189 - builder: (value) => Text(  
190 - '${value.counter}', //here  
191 - ),  
192 -), 168 +// First parameter: condition, must return true of false
  169 +// Second parameter: the new value to aplly if the condition is true
  170 +list.addIf(item < limit, item);
193 ``` 171 ```
194 172
195 -You may also need an instance of your controller outside of your GetBuilder, and you can use these approaches to achieve this: 173 +Without decorations, without a code generator, without complications :smile:
  174 +
  175 +Do you know Flutter's counter app? Your Controller class might look like this:
196 176
197 ```dart 177 ```dart
198 -class Controller extends GetxController {  
199 - static Controller get to => Get.find();  
200 -[...] 178 +class CountCtl extends GetxController {
  179 + final count = 0.obs;
201 } 180 }
202 -// on you view:  
203 -GetBuilder<Controller>(  
204 - init: Controller(), // use it only first time on each controller  
205 - builder: (_) => Text(  
206 - '${Controller.to.counter}', //here  
207 - )  
208 -),  
209 ``` 181 ```
210 182
211 -or 183 +With a simple:
212 184
213 ```dart 185 ```dart
214 -class Controller extends GetxController {  
215 - // static Controller get to => Get.find(); // with no static get  
216 -[...]  
217 -}  
218 -// on stateful/stateless class  
219 -GetBuilder<Controller>(  
220 - init: Controller(), // use it only first time on each controller  
221 - builder: (_) => Text(  
222 - '${Get.find<Controller>().counter}', //here  
223 - ),  
224 -), 186 +ctl.count.value++
225 ``` 187 ```
226 188
227 -- You can use "non-canonical" approaches to do this. If you are using some other dependency manager, like get_it, modular, etc., and just want to deliver the controller instance, you can do this: 189 +You could update the counter variable in your UI, regardless of where it is stored.
228 190
229 -```dart  
230 -Controller controller = Controller();  
231 -[...]  
232 -GetBuilder<Controller>(  
233 - init: controller, //here  
234 - builder: (_) => Text(  
235 - '${controller.counter}', // here  
236 - ),  
237 -), 191 +### Where .obs can be used
238 192
239 -``` 193 +You can transform anything on obs:
240 194
241 -### Unique IDs 195 +```dart
  196 +class RxUser {
  197 + final name = "Camila".obs;
  198 + final age = 18.obs;
  199 +}
242 200
243 -If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs: 201 +class User {
  202 + User({String name, int age});
  203 + final rx = RxUser();
244 204
245 -```dart  
246 -GetBuilder<Controller>(  
247 - id: 'text'  
248 - init: Controller(), // use it only first time on each controller  
249 - builder: (_) => Text(  
250 - '${Get.find<Controller>().counter}', //here  
251 - ),  
252 -),  
253 -``` 205 + String get name => rx.name.value;
  206 + set name(String value) => rx.name.value = value;
254 207
255 -And update it this form: 208 + int get age => rx.age.value;
  209 + set age(int value) => rx.age.value = value;
  210 +}
  211 +```
256 212
257 ```dart 213 ```dart
258 -update(['text']); 214 +
  215 +void main() {
  216 + final user = User();
  217 + print(user.name);
  218 + user.age = 23;
  219 + user.rx.age.listen((int age) => print(age));
  220 + user.age = 24;
  221 + user.age = 25;
  222 +}
  223 +___________
  224 +out:
  225 +Camila
  226 +23
  227 +24
  228 +25
  229 +
259 ``` 230 ```
260 231
261 -You can also impose conditions for the update: 232 +### Note about Lists
  233 +
  234 +Lists are completely observable as are the objects within it. That way, if you add a value to a list, it will automatically rebuild the widgets that use it.
  235 +
  236 +You also don't need to use ".value" with lists, the amazing dart api allowed us to remove that, unfortunate primitive types like String and int cannot be extended, making the use of .value mandatory, but that won't be a problem if you work with gets and setters for these.
262 237
263 ```dart 238 ```dart
264 -update(['text'], counter < 10); 239 +final String title = 'User Info:'.obs
  240 +final list = List<User>().obs;
265 ``` 241 ```
266 242
267 -GetX does this automatically and only reconstructs the widget that uses the exact variable that was changed, if you change a variable to the same as the previous one and that does not imply a change of state , GetX will not rebuild the widget to save memory and CPU cycles (3 is being displayed on the screen, and you change the variable to 3 again. In most state managers, this will cause a new rebuild, but with GetX the widget will only is rebuilt again, if in fact his state has changed). 243 +```dart
  244 +Text(title.value), // String need to have .value in front of it
  245 +ListView.builder (
  246 + itemCount: list.lenght // lists don't need it
  247 +)
  248 +```
268 249
269 -## Reactive State Manager 250 +You don't have to work with sets if you don't want to. you can use the "assign 'and" assignAll "api.
  251 +The "assign" api will clear your list, and add a single object that you want to start there.
  252 +The "assignAll" api will clear the existing list and add any iterable objects that you inject into it.
270 253
271 -Reactive programming can alienate many people because it is said to be complicated. Getx turns reactive programming into something so simple, that it can be used and learned by those who started at that very moment in Flutter. No, you will not need to create StreamControllers. You also won't need to create a StreamBuilder for each variable. You will not need to create a class for each state. You will not need to create a get for an initial value. Reactive programming with Get is as easy as using setState (or even easier!). 254 +### Why i have to use .value
272 255
273 -Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed. 256 +We could remove the obligation to use 'value' to String and int with a simple decoration and code generator, but the purpose of this lib is precisely not to need any external dependency. It is to offer an environment ready for programming, involving the essentials (management of routes, dependencies and states), in a simple, light and performance way without needing any external package.
274 257
275 -This is your count variable: 258 +You can literally add 3 letters to your pubspec (get) and a colon and start programming. All solutions included by default, from route management to state management, aim at ease, productivity and performance.
276 259
277 -```dart  
278 -var name = 'Jonatas Borges';  
279 -``` 260 +The total weight of this library is less than that of a single state manager, even though it is a complete solution, and that is what you must understand.
280 261
281 -To make it observable, you just need to add ".obs" to the end of it: 262 +If you are bothered by value, and like a code generator, MobX is a great alternative, and you can use it in conjunction with Get. For those who want to add a single dependency in pubspec and start programming without worrying about the version of a package being incompatible with another, or if the error of a state update is coming from the state manager or dependency, or still, do not want to worrying about the availability of controllers, whether literally "just programming", get is just perfect.
282 263
283 -```dart  
284 -var name = 'Jonatas Borges'.obs;  
285 -``` 264 +If you have no problem with the MobX code generator, or have no problem with the BLoC boilerplate, you can simply use Get for routes, and forget that it has state manager. Get SEM and RSM were born out of necessity, my company had a project with more than 90 controllers, and the code generator simply took more than 30 minutes to complete its tasks after a Flutter Clean on a reasonably good machine, if your project it has 5, 10, 15 controllers, any state manager will supply you well. If you have an absurdly large project, and code generator is a problem for you, you have been awarded this solution.
286 265
287 -This borders on absurdity when it comes to practicality. What did we do under the hood? We created a stream of Strings, assigned the initial value "Jonatas Borges", we warn all widgets that use "Jonatas Borges" that they now belong to this variable, and when it is changed, they will be changed too. This is the magic of Get, that only dart allows us to do this. 266 +Obviously, if someone wants to contribute to the project and create a code generator, or something similar, I will link in this readme as an alternative, my need is not the need for all devs, but for now I say, there are good solutions that already do that, like MobX.
288 267
289 -Okay, but as we know, a widget can only be changed if it is inside a function, because static classes do not have the power to "auto-change". You will need to create a StreamBuilder, subscribe to listen to this variable, and create a "cascade" of StreamBuilder if you want to change several variables in the same scope, right?  
290 -No, you don't need a StreamBuilder, but you are right about static classes. 268 +### Obx()
291 269
292 -Well, in the view we usually have a lot of boilerplate when we want to change a specific widget. With Get you can also forget about this Boilerplate. StreamBuilder? initialValue? builder?  
293 -No, you just need to play this variable inside an Obx widget. 270 +Typing in Get using Bindings is unnecessary. you can use the Obx widget instead of GetX which only receives the anonymous function that creates a widget.
  271 +Obviously, if you don't use a type, you will need to have an instance of your controller to use the variables, or use `Get.find<Controller>()` .value or Controller.to.value to retrieve the value.
  272 +
  273 +### Workers
  274 +
  275 +Workers will assist you, triggering specific callbacks when an event occurs.
294 276
295 ```dart 277 ```dart
296 -Obx (() => Text (controller.name)); 278 +/// Called every time the variable $_ is changed
  279 +ever(count1, (_) => print("$_ has been changed"));
  280 +
  281 +/// Called only first time the variable $_ is changed
  282 +once(count1, (_) => print("$_ was changed once"));
  283 +
  284 +/// Anti DDos - Called every time the user stops typing for 1 second, for example.
  285 +debounce(count1, (_) => print("debouce$_"), time: Duration(seconds: 1));
  286 +
  287 +/// Ignore all changes within 1 second.
  288 +interval(count1, (_) => print("interval $_"), time: Duration(seconds: 1));
297 ``` 289 ```
298 290
299 -What do you need to memorize? "Obx(() =>" You are just passing that widget through an arrow-function into an Obx. Obx is smart, and will only be changed if the value of name is changed. If name is "John" and you change it to "John", it will not have any changes on the screen, and Obx will simply ignore this change, and will not rebuild the widget, to save resources. Isn't that amazing? 291 +- ever
  292 +'ever' is called every time its variable is changed. That's it.
300 293
301 -What if I have 5 observable variables within an Obx? It will update when any of them are changed. And if I have 30 variables in a class, when I update one, will it update all variables that are in that class? No, just the specific widget that uses that variable. And if I machine-gun my observable variable 1 billion times with the same value, will I have freeze on the screen for unnecessary reconstructions? No, GetX only updates the screen when the variable changes on the screen, if the screen remains the same, it will not reconstruct anything. 294 +- ever
  295 +'once' is called only the first time the variable has been changed.
302 296
303 -### Advantages 297 +- debounce
  298 +'debounce' is very useful in search functions, where you only want the API to be called when the user finishes typing. If the user types "Jonny", you will have 5 searches in the APIs, by the letter J, o, n, n, and y. With Get this does not happen, because you will have a "debounce" Worker that will only be triggered at the end of typing.
304 299
305 -GetBuilder is aimed precisely at multiple state control. Imagine that you added 30 products to a cart, you click delete one, at the same time that the list is updated, the price is updated and the badge in the shopping cart is updated to a smaller number. This type of approach makes GetBuilder killer, because it groups states and changes them all at once without any "computational logic" for that. GetBuilder was created with this type of situation in mind, since for ephemeral change of state, you can use setState and you would not need a state manager for this. However, there are situations where you want only the widget where a certain variable has been changed to be rebuilt, and this is what GetX does with a mastery never seen before. 300 +- interval
  301 +'interval' is different from the debouce. debouce if the user makes 1000 changes to a variable within 1 second, he will send only the last one after the stipulated timer (the default is 800 milliseconds). Interval will instead ignore all user actions for the stipulated period. If you send events for 1 minute, 1000 per second, debounce will only send you the last one, when the user stops strafing events. interval will deliver events every second, and if set to 3 seconds, it will deliver 20 events that minute. This is recommended to avoid abuse, in functions where the user can quickly click on something and get some advantage (imagine that the user can earn coins by clicking on something, if he clicked 300 times in the same minute, he would have 300 coins, using interval, you you can set a time frame for 3 seconds, and even then clicking 300 or a thousand times, the maximum he would get in 1 minute would be 20 coins, clicking 300 or 1 million times). The debounce is suitable for anti-DDos, for functions like search where each change to onChange would cause a query to your api. Debounce will wait for the user to stop typing the name, to make the request. If it were used in the coin scenario mentioned above, the user would only win 1 coin, because it is only executed, when the user "pauses" for the established time.
306 302
307 -That way, if you want an individual controller, you can assign IDs for that, or use GetX. This is up to you, remembering that the more "individual" widgets you have, the more the performance of GetX will stand out, while the performance of GetBuilder should be superior, when there is multiple change of state. 303 +## Mixing the two state managers
308 304
309 -You can use both in any situation, but if you want to tune their application to the maximum possible performance, I would say that: if your variables are changed at different times, use GetX, because there is no competition for it when the subject is to rebuild only what is necessary, if you do not need unique IDs, because all your variables will be changed when you perform an action, use GetBuilder, because it is a simple state updater in blocks, made in a few lines of code, to make just what he promises to do: update state in blocks. There is no way to compare RAM, CPU, or anything else from a giant state manager to a simple StatefulWidget (like GetBuilder) that is updated when you call update(). It was done in a simple way, to have the least computational logic involved, just to fulfill a single purpose and spend the minimum resources possible for that purpose.  
310 -If you want a powerful state manager, you can go without fear to GetX. It does not work with variables, but flows, everything in it is streams under the hood. You can use rxDart in conjunction with it, because everything is stream, you can hear the event of each "variable", because everything in it is stream, it is literally BLoC, easier than MobX, and without code generator or decorations . 305 +Some people opened a feature request, as they wanted to use only one type of reactive variable, and the other mechanics, and needed to insert an Obx into a GetBuilder for this. Thinking about it MixinBuilder was created. It allows both reactive changes by changing ".obs" variables, and mechanical updates via update(). However, of the 4 widgets he is the one that consumes the most resources, since in addition to having a Subscription to receive change events from his children, he subscribes to the update method of his controller.
311 306
312 -If you want power, Get gives you the most advanced state manager you could ever have.  
313 -GetX was built 100% based on Streams, and give you all the firepower that BLoC gave you, with an easier facility than using MobX.  
314 -Without decorations, you can turn anything into Observable with just a ".obs". 307 +Extending GetxController is important, as they have life cycles, and can "start" and "end" events in their onInit() and onClose() methods. You can use any class for this, but I strongly recommend you use the GetxController class to place your variables, whether they are observable or not.
315 308
316 -Maximum performance: In addition to having a smart algorithm for minimal reconstruction, Get uses comparators to make sure the state has changed. If you experience any errors in your application, and send a duplicate change of state, Get will ensure that your application does not collapse.  
317 -The state only changes if the values ​​change. That's the main difference between Get, and using Computed from MobX. When joining two observables, when one is changed, the hearing of that observable will change. With Get, if you join two variables (which is unnecessary computed for that), GetX (similar to Observer) will only change if it implies a real change of state. 309 +## Simple State Manager
318 310
319 -### Usage 311 +Get has a state manager that is extremely light and easy, which does not use ChangeNotifier, will meet the need especially for those new to Flutter, and will not cause problems for large applications.
320 312
321 -You have 3 ways to turn a variable into an observable.  
322 -The first is using Rx{Type}. 313 +GetBuilder is aimed precisely at multiple state control. Imagine that you added 30 products to a cart, you click delete one, at the same time that the list is updated, the price is updated and the badge in the shopping cart is updated to a smaller number. This type of approach makes GetBuilder killer, because it groups states and changes them all at once without any "computational logic" for that. GetBuilder was created with this type of situation in mind, since for ephemeral change of state, you can use setState and you would not need a state manager for this.
323 314
324 -```dart  
325 -var count = RxString();  
326 -``` 315 +That way, if you want an individual controller, you can assign IDs for that, or use GetX. This is up to you, remembering that the more "individual" widgets you have, the more the performance of GetX will stand out, while the performance of GetBuilder should be superior, when there is multiple change of state.
327 316
328 -The second is to use Rx and type it with `Rx<Type>` 317 +### Advantages
329 318
330 -```dart  
331 -var count = Rx<String>();  
332 -``` 319 +1. Update only the required widgets.
333 320
334 -The third, more practical, easy, and incredible approach, is just to add an .obs to your variable. 321 +2. Does not use changeNotifier, it is the state manager that uses less memory (close to 0mb).
335 322
336 -```dart  
337 -var count = 0.obs; 323 +3. Forget StatefulWidget! With Get you will never need it. With the other state managers, you will probably have to use a StatefulWidget to get the instance of your Provider, BLoC, MobX Controller, etc. But have you ever stopped to think that your appBar, your scaffold, and most of the widgets that are in your class are stateless? So why save the state of an entire class, if you can only save the state of the Widget that is stateful? Get solves that, too. Create a Stateless class, make everything stateless. If you need to update a single component, wrap it with GetBuilder, and its state will be maintained.
338 324
339 -// or Rxint count = 0.obs;  
340 -// or Rx<int> count = 0.obs;  
341 -``` 325 +4. Organize your project for real! Controllers must not be in your UI, place your TextEditController, or any controller you use within your Controller class.
342 326
343 - As we know, Dart is now heading towards null safety. With that it is a good idea, from now on, you start to use your variables always with an initial value. Transforming a variable into an observable with an initial value with Get is the simplest and most practical approach that currently exists in Flutter. You will literally add a ".obs" to the end of your variable, and that’s it, you’ve made it observable, and its value will be the initial value, this is fantastic! 327 +5. Do you need to trigger an event to update a widget as soon as it is rendered? GetBuilder has the property "initState", just like StatefulWidget, and you can call events from your controller, directly from it, no more events being placed in your initState.
344 328
345 -You can add variables, and if you want to type your widget to get your controller inside, you just need to use GetX widget instead of Obx 329 +6. Do you need to trigger an action like closing streams, timers and etc? GetBuilder also has the dispose property, where you can call events as soon as that widget is destroyed.
346 330
347 -```dart  
348 -final count1 = 0.obs;  
349 -final count2 = 0.obs;  
350 -int get sum => count1.value + count2.value;  
351 -``` 331 +7. Use streams only if necessary. You can use your StreamControllers inside your controller normally, and use StreamBuilder also normally, but remember, a stream reasonably consumes memory, reactive programming is beautiful, but you shouldn't abuse it. 30 streams open simultaneously can be worse than changeNotifier (and changeNotifier is very bad).
  332 +
  333 +8. Update widgets without spending ram for that. Get stores only the GetBuilder creator ID, and updates that GetBuilder when necessary. The memory consumption of the get ID storage in memory is very low even for thousands of GetBuilders. When you create a new GetBuilder, you are actually sharing the state of GetBuilder that has a creator ID. A new state is not created for each GetBuilder, which saves A LOT OF ram for large applications. Basically your application will be entirely Stateless, and the few Widgets that will be Stateful (within GetBuilder) will have a single state, and therefore updating one will update them all. The state is just one.
  334 +
  335 +9. Get is omniscient and in most cases it knows exactly the time to take a controller out of memory. You should not worry about when to dispose of a controller, Get knows the best time to do this.
  336 +
  337 +### Usage
352 338
353 ```dart 339 ```dart
354 -GetX<Controller>(  
355 - builder: (value) {  
356 - print("count 1 rebuild");  
357 - return Text('${value.count1.value}');  
358 - },  
359 -),  
360 -GetX<Controller>(  
361 - builder: (_) {  
362 - print("count 2 rebuild");  
363 - return Text('${_.count2.value}');  
364 - },  
365 -),  
366 -GetX<Controller>(  
367 - builder: (_) {  
368 - print("count 3 rebuild");  
369 - return Text('${_.sum}');  
370 - },  
371 -), 340 +// Create controller class and extends GetxController
  341 +class Controller extends GetxController {
  342 + int counter = 0;
  343 + void increment() {
  344 + counter++;
  345 + update(); // use update() to update counter variable on UI when increment be called
  346 + }
  347 +}
  348 +// On your Stateless/Stateful class, use GetBuilder to update Text when increment be called
  349 +GetBuilder<Controller>(
  350 + init: Controller(), // INIT IT ONLY THE FIRST TIME
  351 + builder: (_) => Text(
  352 + '${_.counter}',
  353 + ),
  354 +)
  355 +//Initialize your controller only the first time. The second time you are using ReBuilder for the same controller, do not use it again. Your controller will be automatically removed from memory as soon as the widget that marked it as 'init' is deployed. You don't have to worry about that, Get will do it automatically, just make sure you don't start the same controller twice.
372 ``` 356 ```
373 357
374 -If we increment the number of count 1, only count 1 and count 3 are reconstructed, because count 1 now has a value of 1, and 1 + 0 = 1, changing the sum value. 358 +**Done!**
375 359
376 -If we change count 2, only count2 and 3 are reconstructed, because the value of 2 has changed, and the result of the sum is now 2. 360 +- You have already learned how to manage states with Get.
377 361
378 -If we add the number 1 to count 1, which already contains 1, no widget is reconstructed. If we add a value of 1 for count 1 and a value of 2 for count 2, only 2 and 3 will be reconstructed, simply because GetX not only changes what is necessary, it avoids duplicating events. 362 +- Note: You may want a larger organization, and not use the init property. For that, you can create a class and extends Bindings class, and within it mention the controllers that will be created within that route. Controllers will not be created at that time, on the contrary, this is just a statement, so that the first time you use a Controller, Get will know where to look. Get will remain lazyLoad, and will continue to dispose Controllers when they are no longer needed. See the pub.dev example to see how it works.
379 363
380 -- NOTE: By default, the first event will allow rebuild even if it is the same. We created this behavior due to dualistic variables, such as Boolean.  
381 -Imagine you did this: 364 +If you navigate many routes and need data that was in your previously used controller, you just need to use GetBuilder Again (with no init):
382 365
383 ```dart 366 ```dart
384 -var isLogged = false.obs; 367 +class OtherClass extends StatelessWidget {
  368 + @override
  369 + Widget build(BuildContext context) {
  370 + return Scaffold(
  371 + body: Center(
  372 + child: GetBuilder<Controller>(
  373 + builder: (s) => Text('${s.counter}'),
  374 + ),
  375 + ),
  376 + );
  377 + }
  378 +
385 ``` 379 ```
386 380
387 -and then you check if a user is logged in to trigger an event in "ever". 381 +If you need to use your controller in many other places, and outside of GetBuilder, just create a get in your controller and have it easily. (or use `Get.find<Controller>()`)
388 382
389 ```dart 383 ```dart
390 -onInit(){  
391 - ever(isLogged, fireRoute);  
392 - isLogged.value = await Preferences.hasToken();  
393 -} 384 +class Controller extends GetxController {
394 385
395 -fireRoute(logged) {  
396 - if (logged) {  
397 - Get.off(Home());  
398 - } else {  
399 - Get.off(Login()); 386 + /// You do not need that. I recommend using it just for ease of syntax.
  387 + /// with static method: Controller.to.counter();
  388 + /// with no static method: Get.find<Controller>().counter();
  389 + /// There is no difference in performance, nor any side effect of using either syntax. Only one does not need the type, and the other the IDE will autocomplete it.
  390 + static Controller get to => Get.find(); // add this line
  391 +
  392 + int counter = 0;
  393 + void increment() {
  394 + counter++;
  395 + update();
400 } 396 }
401 } 397 }
402 ``` 398 ```
403 399
404 -if hasToken was false, there would be no change to isLogged, so ever would never be called. To avoid this type of behavior, the first change to an observable will always trigger an event, even if it is the same.  
405 -You can remove this behavior if you want, using:  
406 -`isLogged.firstRebuild = false;`  
407 -  
408 -In addition, Get provides refined state control. You can condition an event (such as adding an object to a list), on a certain condition. 400 +And then you can access your controller directly, that way:
409 401
410 ```dart 402 ```dart
411 -list.addIf(item<limit, item); 403 +FloatingActionButton(
  404 + onPressed: () {
  405 + Controller.to.increment(),
  406 + } // This is incredibly simple!
  407 + child: Text("${Controller.to.counter}"),
  408 +),
412 ``` 409 ```
413 410
414 -Without decorations, without a code generator, without complications, GetX will change the way you manage your states in Flutter, and that is not a promise, it is a certainty! 411 +When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically.
415 412
416 -Do you know Flutter's counter app? Your Controller class might look like this: 413 +### How it handles controllers
417 414
418 -```dart  
419 -class CountCtl extends GetxController {  
420 - final count = 0.obs;  
421 -}  
422 -``` 415 +Let's say we have this:
423 416
424 -With a simple: 417 +`Class a => Class B (has controller X) => Class C (has controller X)`
425 418
426 -```dart  
427 -ctl.count.value++  
428 -``` 419 +In class A the controller is not yet in memory, because you have not used it yet (Get is lazyLoad). In class B you used the controller, and it entered memory. In class C you used the same controller as in class B, Get will share the state of controller B with controller C, and the same controller is still in memory. If you close screen C and screen B, Get will automatically take controller X out of memory and free up resources, because Class a is not using the controller. If you navigate to B again, controller X will enter memory again, if instead of going to class C, you return to class A again, Get will take the controller out of memory in the same way. If class C didn't use the controller, and you took class B out of memory, no class would be using controller X and likewise it would be disposed of. The only exception that can mess with Get, is if you remove B from the route unexpectedly, and try to use the controller in C. In this case, the creator ID of the controller that was in B was deleted, and Get was programmed to remove it from memory every controller that has no creator ID. If you intend to do this, add the "autoRemove: false" flag to class B's GetBuilder and use adoptID = true; in class C's GetBuilder.
429 420
430 -You could update the counter variable in your UI, regardless of where it is stored. 421 +### You won't need StatefulWidgets anymore
431 422
432 -### Where .obs can be used 423 +Using StatefulWidgets means storing the state of entire screens unnecessarily, even because if you need to minimally rebuild a widget, you will embed it in a Consumer/Observer/BlocProvider/GetBuilder/GetX/Obx, which will be another StatefulWidget.
  424 +The StatefulWidget class is a class larger than StatelessWidget, which will allocate more RAM, and this may not make a significant difference between one or two classes, but it will most certainly do when you have 100 of them!
  425 +Unless you need to use a mixin, like TickerProviderStateMixin, it will be totally unnecessary to use a StatefulWidget with Get.
433 426
434 -You can transform anything on obs: 427 +You can call all methods of a StatefulWidget directly from a GetBuilder.
  428 +If you need to call initState() or dispose() method for example, you can call them directly;
435 429
436 ```dart 430 ```dart
437 -class RxUser {  
438 - final name = "Camila".obs;  
439 - final age = 18.obs;  
440 -}  
441 -  
442 -class User {  
443 - User({String name, int age});  
444 - final rx = RxUser(); 431 +GetBuilder<Controller>(
  432 + initState: (_) => Controller.to.fetchApi(),
  433 + dispose: (_) => Controller.to.closeStreams(),
  434 + builder: (s) => Text('${s.username}'),
  435 +),
  436 +```
445 437
446 - String get name => rx.name.value;  
447 - set name(String value) => rx.name.value = value; 438 +A much better approach than this is to use the onInit() and onClose() method directly from your controller.
448 439
449 - int get age => rx.age.value;  
450 - set age(int value) => rx.age.value = value; 440 +```dart
  441 +@override
  442 +void onInit() {
  443 + fetchApi();
  444 + super.onInit();
451 } 445 }
452 ``` 446 ```
453 447
454 -```dart 448 +- NOTE: If you want to start a method at the moment the controller is called for the first time, you DON'T NEED to use constructors for this, in fact, using a performance-oriented package like Get, this borders on bad practice, because it deviates from the logic in which the controllers are created or allocated (if you create an instance of this controller, the constructor will be called immediately, you will be populating a controller before it is even used, you are allocating memory without it being in use, this definitely hurts the principles of this library). The onInit() methods; and onClose(); were created for this, they will be called when the Controller is created, or used for the first time, depending on whether you are using Get.lazyPut or not. If you want, for example, to make a call to your API to populate data, you can forget about the old-fashioned method of initState/dispose, just start your call to the api in onInit, and if you need to execute any command like closing streams, use the onClose() for that.
455 449
456 -void main() {  
457 - final user = User();  
458 - print(user.name);  
459 - user.age = 23;  
460 - user.rx.age.listen((int age) => print(age));  
461 - user.age = 24;  
462 - user.age = 25;  
463 -}  
464 -___________  
465 -out:  
466 -Camila  
467 -23  
468 -24  
469 -25 450 +### Why it exists
470 451
471 -``` 452 +The purpose of this package is precisely to give you a complete solution for navigation of routes, management of dependencies and states, using the least possible dependencies, with a high degree of decoupling. Get engages all high and low level Flutter APIs within itself, to ensure that you work with the least possible coupling. We centralize everything in a single package, to ensure that you don't have any kind of coupling in your project. That way, you can put only widgets in your view, and leave the part of your team that works with the business logic free, to work with the business logic without depending on any element of the View. This provides a much cleaner working environment, so that part of your team works only with widgets, without worrying about sending data to your controller, and part of your team works only with the business logic in its breadth, without depending on no element of the view.
472 453
473 -### Note about Lists 454 +So to simplify this:
  455 +You don't need to call methods in initState and send them by parameter to your controller, nor use your controller constructor for that, you have the onInit() method that is called at the right time for you to start your services.
  456 +You do not need to call the device, you have the onClose() method that will be called at the exact moment when your controller is no longer needed and will be removed from memory. That way, leave views for widgets only, refrain from any kind of business logic from it.
474 457
475 -Working with Lists using Get is the best and most enjoyable thing in the world. They are completely observable as are the objects within it. That way, if you add a value to a list, it will automatically rebuild the widgets that use it.  
476 -You also don't need to use ".value" with lists, the amazing dart api allowed us to remove that, unfortunate primitive types like String and int cannot be extended, making the use of .value mandatory, but that won't be a problem if you work with gets and setters for these. 458 +Do not call a dispose method inside GetxController, it will not do anything, remember that the controller is not a Widget, you should not "dispose" it, and it will be automatically and intelligently removed from memory by Get. If you used any stream on it and want to close it, just insert it into the close method. Example:
477 459
478 ```dart 460 ```dart
479 -final list = List<User>().obs;  
480 -``` 461 +class Controller extends GetxController {
  462 + StreamController<User> user = StreamController<User>();
  463 + StreamController<String> name = StreamController<String>();
481 464
482 -```dart  
483 -ListView.builder (  
484 - itemCount: list.lenght  
485 -) 465 + /// close stream = onClose method, not dispose.
  466 + @override
  467 + void onClose() {
  468 + user.close();
  469 + name.close();
  470 + super.onClose();
  471 + }
  472 +}
486 ``` 473 ```
487 474
488 -You don't have to work with sets if you don't want to. you can use the "assign 'and" assignAll "api.  
489 -The "assign" api will clear your list, and add a single object that you want to start there.  
490 -The "assignAll" api will clear the existing list and add any iterable objects that you inject into it.  
491 -  
492 -### Why i have to use .value 475 +Controller life cycle:
493 476
494 -We could remove the obligation to use 'value' to String and int with a simple decoration and code generator, but the purpose of this lib is precisely not to need any external dependency. It is to offer an environment ready for programming, involving the essentials (management of routes, dependencies and states), in a simple, light and performance way without needing any external package. You can literally add 3 letters to your pubspec (get) and start programming. All solutions included by default, from route management to state management, aim at ease, productivity and performance. The total weight of this library is less than that of a single state manager, even though it is a complete solution, and that is what you must understand. If you are bothered by value, and like a code generator, MobX is a great alternative, and you can use it in conjunction with Get. For those who want to add a single dependency in pubspec and start programming without worrying about the version of a package being incompatible with another, or if the error of a state update is coming from the state manager or dependency, or still, do not want to worrying about the availability of controllers, whether literally "just programming", get is just perfect. 477 +- onInit() where it is created.
  478 +- onClose() where it is closed to make any changes in preparation for the delete method
  479 +- deleted: you do not have access to this API because it is literally removing the controller from memory. It is literally deleted, without leaving any trace.
495 480
496 -If you have no problem with the MobX code generator, or have no problem with the BLoC boilerplate, you can simply use Get for routes, and forget that it has state manager. Get SEM and RSM were born out of necessity, my company had a project with more than 90 controllers, and the code generator simply took more than 30 minutes to complete its tasks after a Flutter Clean on a reasonably good machine, if your project it has 5, 10, 15 controllers, any state manager will supply you well. If you have an absurdly large project, and code generator is a problem for you, you have been awarded this solution. 481 +### Other ways of using it
497 482
498 -Obviously, if someone wants to contribute to the project and create a code generator, or something similar, I will link in this readme as an alternative, my need is not the need for all devs, but for now I say, there are good solutions that already do that, like MobX. 483 +You can use Controller instance directly on GetBuilder value:
499 484
500 -### Obx() 485 +```dart
  486 +GetBuilder<Controller>(
  487 + init: Controller(),
  488 + builder: (value) => Text(
  489 + '${value.counter}', //here
  490 + ),
  491 +),
  492 +```
501 493
502 -Typing in Get using Bindings is unnecessary. you can use the Obx widget instead of GetX which only receives the anonymous function that creates a widget.  
503 -Obviously, if you don't use a type, you will need to have an instance of your controller to use the variables, or use `Get.find<Controller>()` .value or Controller.to.value to retrieve the value. 494 +You may also need an instance of your controller outside of your GetBuilder, and you can use these approaches to achieve this:
504 495
505 -### Workers 496 +```dart
  497 +class Controller extends GetxController {
  498 + static Controller get to => Get.find();
  499 +[...]
  500 +}
  501 +// on you view:
  502 +GetBuilder<Controller>(
  503 + init: Controller(), // use it only first time on each controller
  504 + builder: (_) => Text(
  505 + '${Controller.to.counter}', //here
  506 + )
  507 +),
  508 +```
506 509
507 -Workers will assist you, triggering specific callbacks when an event occurs. 510 +or
508 511
509 ```dart 512 ```dart
510 -/// Called every time the variable $_ is changed  
511 -ever(count1, (_) => print("$_ has been changed")); 513 +class Controller extends GetxController {
  514 + // static Controller get to => Get.find(); // with no static get
  515 +[...]
  516 +}
  517 +// on stateful/stateless class
  518 +GetBuilder<Controller>(
  519 + init: Controller(), // use it only first time on each controller
  520 + builder: (_) => Text(
  521 + '${Get.find<Controller>().counter}', //here
  522 + ),
  523 +),
  524 +```
512 525
513 -/// Called only first time the variable $_ is changed  
514 -once(count1, (_) => print("$_ was changed once")); 526 +- You can use "non-canonical" approaches to do this. If you are using some other dependency manager, like get_it, modular, etc., and just want to deliver the controller instance, you can do this:
515 527
516 -/// Anti DDos - Called every time the user stops typing for 1 second, for example.  
517 -debounce(count1, (_) => print("debouce$_"), time: Duration(seconds: 1)); 528 +```dart
  529 +Controller controller = Controller();
  530 +[...]
  531 +GetBuilder<Controller>(
  532 + init: controller, //here
  533 + builder: (_) => Text(
  534 + '${controller.counter}', // here
  535 + ),
  536 +),
518 537
519 -/// Ignore all changes within 1 second.  
520 -interval(count1, (_) => print("interval $_"), time: Duration(seconds: 1));  
521 ``` 538 ```
522 539
523 -- ever  
524 -'ever' is called every time its variable is changed. That's it. 540 +### Unique IDs
525 541
526 -- ever  
527 -'once' is called only the first time the variable has been changed. 542 +If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs:
528 543
529 -- debounce  
530 -'debounce' is very useful in search functions, where you only want the API to be called when the user finishes typing. If the user types "Jonny", you will have 5 searches in the APIs, by the letter J, o, n, n, and y. With Get this does not happen, because you will have a "debounce" Worker that will only be triggered at the end of typing. 544 +```dart
  545 +GetBuilder<Controller>(
  546 + id: 'text'
  547 + init: Controller(), // use it only first time on each controller
  548 + builder: (_) => Text(
  549 + '${Get.find<Controller>().counter}', //here
  550 + ),
  551 +),
  552 +```
531 553
532 -- interval  
533 -'interval' is different from the debouce. debouce if the user makes 1000 changes to a variable within 1 second, he will send only the last one after the stipulated timer (the default is 800 milliseconds). Interval will instead ignore all user actions for the stipulated period. If you send events for 1 minute, 1000 per second, debounce will only send you the last one, when the user stops strafing events. interval will deliver events every second, and if set to 3 seconds, it will deliver 20 events that minute. This is recommended to avoid abuse, in functions where the user can quickly click on something and get some advantage (imagine that the user can earn coins by clicking on something, if he clicked 300 times in the same minute, he would have 300 coins, using interval, you you can set a time frame for 3 seconds, and even then clicking 300 or a thousand times, the maximum he would get in 1 minute would be 20 coins, clicking 300 or 1 million times). The debounce is suitable for anti-DDos, for functions like search where each change to onChange would cause a query to your api. Debounce will wait for the user to stop typing the name, to make the request. If it were used in the coin scenario mentioned above, the user would only win 1 coin, because it is only executed, when the user "pauses" for the established time. 554 +And update it this form:
534 555
535 -## Mixing the two state managers 556 +```dart
  557 +update(['text']);
  558 +```
536 559
537 -Some people opened a feature request, as they wanted to use only one type of reactive variable, and the other mechanics, and needed to insert an Obx into a GetBuilder for this. Thinking about it MixinBuilder was created. It allows both reactive changes by changing ".obs" variables, and mechanical updates via update(). However, of the 4 widgets he is the one that consumes the most resources, since in addition to having a Subscription to receive change events from his children, he subscribes to the update method of his controller. 560 +You can also impose conditions for the update:
538 561
539 -Extending GetxController is important, as they have life cycles, and can "start" and "end" events in their onInit() and onClose() methods. You can use any class for this, but I strongly recommend you use the GetxController class to place your variables, whether they are observable or not. 562 +```dart
  563 +update(['text'], counter < 10);
  564 +```
  565 +
  566 +GetX does this automatically and only reconstructs the widget that uses the exact variable that was changed, if you change a variable to the same as the previous one and that does not imply a change of state , GetX will not rebuild the widget to save memory and CPU cycles (3 is being displayed on the screen, and you change the variable to 3 again. In most state managers, this will cause a new rebuild, but with GetX the widget will only is rebuilt again, if in fact his state has changed).
540 567
541 ## GetBuilder vs GetX vs Obx vs MixinBuilder 568 ## GetBuilder vs GetX vs Obx vs MixinBuilder
542 569