Showing
1 changed file
with
47 additions
and
18 deletions
1 | -# Dependency management | ||
2 | -- [Simple Instance Manager](#simple-instance-manager) | ||
3 | -- [Options](#options) | ||
4 | -- [Bindings](#bindings) | ||
5 | - - [How to use](#how-to-use) | ||
6 | -- [SmartManagement](#smartmanagement) | 1 | +# Dependency Management |
2 | +- [Dependency Management](#dependency-management) | ||
3 | + - [Usage](#usage) | ||
4 | + - [Instancing methods](#instancing-methods) | ||
5 | + - [Get.put()](#getput) | ||
6 | + - [Get.lazyPut](#getlazyput) | ||
7 | + - [Get.putAsync](#getputasync) | ||
8 | + - [Get.create](#getcreate) | ||
9 | + - [Diferences between methods:](#diferences-between-methods) | ||
10 | + - [Bindings](#bindings) | ||
11 | + - [How to use](#how-to-use) | ||
12 | + - [BindingsBuilder](#bindingsbuilder) | ||
13 | + - [SmartManagement](#smartmanagement) | ||
14 | + - [SmartManagement.full](#smartmanagementfull) | ||
15 | + - [SmartManagement.onlyBuilders](#smartmanagementonlybuilders) | ||
16 | + - [SmartManagement.keepFactory](#smartmanagementkeepfactory) | ||
17 | + - [Notes](#notes) | ||
7 | 18 | ||
8 | Get has a simple and powerful dependency manager that allows you to retrieve the same class as your Bloc or Controller with just 1 lines of code, no Provider context, no inheritedWidget: | 19 | Get has a simple and powerful dependency manager that allows you to retrieve the same class as your Bloc or Controller with just 1 lines of code, no Provider context, no inheritedWidget: |
9 | 20 | ||
@@ -12,8 +23,8 @@ Controller controller = Get.put(Controller()); // Rather Controller controller = | @@ -12,8 +23,8 @@ Controller controller = Get.put(Controller()); // Rather Controller controller = | ||
12 | ``` | 23 | ``` |
13 | 24 | ||
14 | If you don't know why dependency injection is, you can check some links to learn: | 25 | If you don't know why dependency injection is, you can check some links to learn: |
15 | -[Dependency Injection in Flutter](https://medium.com/flutter-community/dependency-injection-in-flutter-f19fb66a0740) | ||
16 | -[Dependency Injection in Dart/Flutter Apps](https://berthacks.com/dependency-injection-in-dartflutter-apps-ck71x93uu06fqd9s1zuosczgn) | 26 | +- [Dependency Injection in Flutter](https://medium.com/flutter-community/dependency-injection-in-flutter-f19fb66a0740) |
27 | +- [Dependency Injection in Dart/Flutter Apps](https://berthacks.com/dependency-injection-in-dartflutter-apps-ck71x93uu06fqd9s1zuosczgn) | ||
17 | 28 | ||
18 | Instead of instantiating your class within the class you are using, you are instantiating it within the Get instance, which will make it available throughout your App. | 29 | Instead of instantiating your class within the class you are using, you are instantiating it within the Get instance, which will make it available throughout your App. |
19 | So you can use your controller (or Bloc class) normally | 30 | So you can use your controller (or Bloc class) normally |
@@ -34,7 +45,7 @@ Text(controller.textFromApi); | @@ -34,7 +45,7 @@ Text(controller.textFromApi); | ||
34 | It is possible to lazyLoad a dependency so that it will be instantiated only when is used. Very useful for computational expensive classes or when you know you will not gonna use that class at that time. | 45 | It is possible to lazyLoad a dependency so that it will be instantiated only when is used. Very useful for computational expensive classes or when you know you will not gonna use that class at that time. |
35 | 46 | ||
36 | ```dart | 47 | ```dart |
37 | -Get.lazyPut<ApiMock>(()=> ApiMock()); | 48 | +Get.lazyPut<ApiMock>(() => ApiMock()); |
38 | /// ApiMock will only be called when someone uses Get.find<ApiMock> for the first time | 49 | /// ApiMock will only be called when someone uses Get.find<ApiMock> for the first time |
39 | ``` | 50 | ``` |
40 | 51 | ||
@@ -127,7 +138,7 @@ Get.lazyPut<S>( | @@ -127,7 +138,7 @@ Get.lazyPut<S>( | ||
127 | Get.lazyPut<FirebaseAuth>( | 138 | Get.lazyPut<FirebaseAuth>( |
128 | () => { | 139 | () => { |
129 | // ... some logic if needed | 140 | // ... some logic if needed |
130 | - FirebaseAuth() | 141 | + return FirebaseAuth() |
131 | }, | 142 | }, |
132 | tag: Math.random().toString(), | 143 | tag: Math.random().toString(), |
133 | fenix: true | 144 | fenix: true |
@@ -298,17 +309,35 @@ That way you can avoid to create one Binding class for each route making this ev | @@ -298,17 +309,35 @@ That way you can avoid to create one Binding class for each route making this ev | ||
298 | 309 | ||
299 | Both ways of doing work perfectly fine and we want you to use what most suit your tastes. | 310 | Both ways of doing work perfectly fine and we want you to use what most suit your tastes. |
300 | 311 | ||
301 | -## SmartManagement | 312 | +### SmartManagement |
313 | + | ||
314 | +GetX by default disposes unused controllers from memory, even if a failure occurs and a widget that uses it is not properly disposed. | ||
315 | +This is what is called the `full` mode of dependency management. | ||
316 | +But if you want to change the way GetX controls the disposal of classes, you have `SmartManagement` class that you can set different behaviors. | ||
317 | + | ||
318 | +#### SmartManagement.full | ||
319 | + | ||
320 | +It is the default one. Dispose classes that are not being used and were not set to be permanent. In the majority of the cases you will want to keep this config untouched. If you new to GetX then don't change this. | ||
321 | + | ||
322 | +#### SmartManagement.onlyBuilders | ||
323 | +With this option, only controllers started in `init:` or loaded into a Binding with `Get.lazyPut` will be disposed. | ||
324 | + | ||
325 | +If you use `Get.put()` or `Get.putAsync()` or any other approach, SmartManagement will not have permissions to exclude this dependency. | ||
302 | 326 | ||
303 | -Always prefer to use standard SmartManagement (full), you do not need to configure anything for that, Get already gives it to you by default. It will surely eliminate all your disused controllers from memory, as its refined control removes the dependency, even if a failure occurs and a widget that uses it is not properly disposed. | ||
304 | -The "full" mode is also safe enough to be used with StatelessWidget, as it has numerous security callbacks that will prevent a controller from remaining in memory if it is not being used by any widget, and disposition is not important here. However, if you are bothered by the default behavior, or just don't want it to happen, Get offers other, more lenient options for intelligent memory management, such as SmartManagement.onlyBuilders, which will depend on the effective removal of widgets using the controller. tree to remove it, and you can prevent a controller from being deployed using "autoRemove: false" in your GetBuilder/GetX. | ||
305 | -With this option, only controllers started in "init:" or loaded into a Binding with "Get.lazyPut" will be disposed, if you use Get.put or any other approach, SmartManagement will not have permissions to exclude this dependency. | ||
306 | With the default behavior, even widgets instantiated with "Get.put" will be removed, unlike SmartManagement.onlyBuilders. | 327 | With the default behavior, even widgets instantiated with "Get.put" will be removed, unlike SmartManagement.onlyBuilders. |
307 | -SmartManagement.keepFactory is like SmartManagement.full, with one difference. SmartManagement.full purges the factories from the premises, so that Get.lazyPut() will only be able to be called once and your factory and references will be self-destructing. SmartManagement.keepFactory will remove its dependencies when necessary, however, it will keep the "shape" of these, to make an equal one if you need an instance of that again. | 328 | + |
329 | +#### SmartManagement.keepFactory | ||
330 | + | ||
331 | +// TODO ASK JONATAS WHAT THE HECK HE MEANT WITH THIS TEXT | ||
332 | +This one is like SmartManagement.full, with one difference: SmartManagement.full purges the factories from the premises, so that Get.lazyPut() will only be able to be called once and your factory and references will be self-destructing. SmartManagement.keepFactory will remove its dependencies when necessary, however, it will keep the "shape" of these, to make an equal one if you need an instance of that again. | ||
333 | + | ||
308 | Instead of using SmartManagement.keepFactory you can use Bindings. | 334 | Instead of using SmartManagement.keepFactory you can use Bindings. |
335 | +// TODO ASK JONATAS THIS TOO | ||
309 | Bindings creates transitory factories, which are created the moment you click to go to another screen, and will be destroyed as soon as the screen-changing animation happens. It is so little time that the analyzer will not even be able to register it. When you navigate to this screen again, a new temporary factory will be called, so this is preferable to using SmartManagement.keepFactory, but if you don't want to create Bindings, or want to keep all your dependencies on the same Binding, it will certainly help you . Factories take up little memory, they don't hold instances, but a function with the "shape" of that class you want. This is very little, but since the purpose of this lib is to get the maximum performance possible using the minimum resources, Get removes even the factories by default. Use whichever is most convenient for you. | 336 | Bindings creates transitory factories, which are created the moment you click to go to another screen, and will be destroyed as soon as the screen-changing animation happens. It is so little time that the analyzer will not even be able to register it. When you navigate to this screen again, a new temporary factory will be called, so this is preferable to using SmartManagement.keepFactory, but if you don't want to create Bindings, or want to keep all your dependencies on the same Binding, it will certainly help you . Factories take up little memory, they don't hold instances, but a function with the "shape" of that class you want. This is very little, but since the purpose of this lib is to get the maximum performance possible using the minimum resources, Get removes even the factories by default. Use whichever is most convenient for you. |
310 | 337 | ||
311 | -- NOTE: DO NOT USE SmartManagement.keepFactory if you are using multiple Bindings. It was designed to be used without Bindings, or with a single Binding linked in the GetMaterialApp's initialBinding. | 338 | +## Notes |
339 | + | ||
340 | +- DO NOT USE SmartManagement.keepFactory if you are using multiple Bindings. It was designed to be used without Bindings, or with a single Binding linked in the GetMaterialApp's initialBinding. | ||
312 | 341 | ||
313 | -- NOTE2: Using Bindings is completely optional, you can use Get.put() and Get.find() on classes that use a given controller without any problem. | ||
314 | -However, if you work with Services or any other abstraction, I recommend using Bindings for a larger organization. | 342 | +- Using Bindings is completely optional, if you want you can use `Get.put()` and `Get.find()` on classes that use a given controller without any problem. |
343 | +However, if you work with Services or any other abstraction, I recommend using Bindings for a better organization. |
-
Please register or login to post a comment