Jonny Borges
Committed by GitHub

Merge pull request #547 from Nipodemos/more-changes-in-dependencies

[docs] more refactor to dependencies docs
1 # Dependency Management 1 # Dependency Management
2 - [Dependency Management](#dependency-management) 2 - [Dependency Management](#dependency-management)
3 - - [Usage](#usage)  
4 - [Instancing methods](#instancing-methods) 3 - [Instancing methods](#instancing-methods)
5 - [Get.put()](#getput) 4 - [Get.put()](#getput)
6 - [Get.lazyPut](#getlazyput) 5 - [Get.lazyPut](#getlazyput)
7 - [Get.putAsync](#getputasync) 6 - [Get.putAsync](#getputasync)
8 - [Get.create](#getcreate) 7 - [Get.create](#getcreate)
9 - - [Differences between methods:](#differences-between-methods) 8 + - [Using instantiated methods/classes](#using-instantiated-methodsclasses)
  9 + - [Differences between methods](#differences-between-methods)
10 - [Bindings](#bindings) 10 - [Bindings](#bindings)
11 - [How to use](#how-to-use) 11 - [How to use](#how-to-use)
12 - [BindingsBuilder](#bindingsbuilder) 12 - [BindingsBuilder](#bindingsbuilder)
@@ -26,60 +26,23 @@ Controller controller = Get.put(Controller()); // Rather Controller controller = @@ -26,60 +26,23 @@ Controller controller = Get.put(Controller()); // Rather Controller controller =
26 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. 26 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.
27 So you can use your controller (or Bloc class) normally 27 So you can use your controller (or Bloc class) normally
28 28
29 -Imagine that you have navigated through numerous routes, and you need a data that was left behind in your controller, you would need a state manager combined with the Provider or Get_it, correct? Not with Get. You just need to ask Get to "find" for your controller, you don't need any additional dependencies:  
30 -  
31 -```dart  
32 -Controller controller = Get.find(); // or final controller = Get.find<Controlelr>();  
33 -//Yes, it looks like Magic, Get will find your controller, and will deliver it to you. You can have 1 million controllers instantiated, Get will always give you the right controller.  
34 -```  
35 -  
36 -And then you will be able to recover your controller data that was obtained back there:  
37 -  
38 -```dart  
39 -Text(controller.textFromApi);  
40 -```  
41 -  
42 -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.  
43 -  
44 -```dart  
45 -Get.lazyPut<ApiMock>(() => ApiMock());  
46 -/// ApiMock will only be called when someone uses Get.find<ApiMock> for the first time  
47 -```  
48 -  
49 -If you want to register an asynchronous instance, you can use `Get.putAsync`:  
50 -  
51 -```dart  
52 -Get.putAsync<SharedPreferences>(() async {  
53 - final prefs = await SharedPreferences.getInstance();  
54 - await prefs.setInt('counter', 12345);  
55 - return prefs;  
56 -});  
57 -```  
58 -  
59 - Note: If you are using Get's State Manager, pay more attention to the [Bindings](#bindings) api, which will make easier to connect your view to your controller. 29 - Note: If you are using Get's State Manager, pay more attention to the [Bindings](#bindings) api, which will make easier to connect your view to your controller.
60 - Note²: Get dependency management is decloupled from other parts of the package, so if for example your app is already using a state manager (any one, it doesn't matter), you don't need to change that, you can use this dependency injection manager with no problems at all 30 - Note²: Get dependency management is decloupled from other parts of the package, so if for example your app is already using a state manager (any one, it doesn't matter), you don't need to change that, you can use this dependency injection manager with no problems at all
61 31
62 -## Usage 32 +## Instancing methods
  33 +The methods and it's configurable parameters are:
63 34
64 -```dart  
65 -int count = Get.find<SharedPreferences>().getInt('counter');  
66 -print(count); // out: 12345  
67 -``` 35 +### Get.put()
68 36
69 -To remove a instance of Get: 37 +The most common way of insert a dependency. Good for the controllers of your views for example.
70 38
71 ```dart 39 ```dart
72 -Get.delete<Controller>(); 40 +Get.put<SomeClass>(SomeClass());
  41 +Get.put<LoginController>(LoginController(), permanent: true);
  42 +Get.put<ListItemController>(ListItemController, tag: "some unique string");
73 ``` 43 ```
74 44
75 -## Instancing methods  
76 -  
77 -Although Getx already delivers very good settings for use, it is possible to refine them even more so that it become more useful to the programmer. The methods and it's configurable parameters are:  
78 -  
79 -### Get.put()  
80 -  
81 -The most common way of inserting a dependency. Good for the controllers of your views for example.  
82 - 45 +This is all options you can set when using put:
83 ```dart 46 ```dart
84 Get.put<S>( 47 Get.put<S>(
85 // mandatory: the class that you want to get to save, like a controller or anything 48 // mandatory: the class that you want to get to save, like a controller or anything
@@ -107,16 +70,28 @@ Get.put<S>( @@ -107,16 +70,28 @@ Get.put<S>(
107 // this one is not commonly used 70 // this one is not commonly used
108 InstanceBuilderCallback<S> builder, 71 InstanceBuilderCallback<S> builder,
109 ) 72 )
110 -  
111 -// Example:  
112 -  
113 -Get.put<LoginController>(LoginController(), permanent: true)  
114 ``` 73 ```
115 74
116 ### Get.lazyPut 75 ### Get.lazyPut
  76 +It is possible to lazyLoad a dependency so that it will be instantiated only when is used. Very useful for computational expensive classes or if you want to instantiate several classes in just one place (like in a Bindings class) and you know you will not gonna use that class at that time.
  77 +
  78 +```dart
  79 +/// ApiMock will only be called when someone uses Get.find<ApiMock> for the first time
  80 +Get.lazyPut<ApiMock>(() => ApiMock());
117 81
118 -With lazyPut, the dependency will be only instantiated when it's called. This is particularly useful if you want to instantiate several classes in just one place, but don't need that instances immediatly 82 +Get.lazyPut<FirebaseAuth>(
  83 + () => {
  84 + // ... some logic if needed
  85 + return FirebaseAuth()
  86 + },
  87 + tag: Math.random().toString(),
  88 + fenix: true
  89 +)
  90 +
  91 +Get.lazyPut<Controller>( () => Controller() )
  92 +```
119 93
  94 +This is all options you can set when using lazyPut:
120 ```dart 95 ```dart
121 Get.lazyPut<S>( 96 Get.lazyPut<S>(
122 // mandatory: a method that will be executed when your class is called for the first time 97 // mandatory: a method that will be executed when your class is called for the first time
@@ -133,25 +108,22 @@ Get.lazyPut<S>( @@ -133,25 +108,22 @@ Get.lazyPut<S>(
133 bool fenix = false 108 bool fenix = false
134 109
135 ) 110 )
136 -  
137 -// example  
138 -Get.lazyPut<FirebaseAuth>(  
139 - () => {  
140 - // ... some logic if needed  
141 - return FirebaseAuth()  
142 - },  
143 - tag: Math.random().toString(),  
144 - fenix: true  
145 -)  
146 -  
147 -// example 2:  
148 -Get.lazyPut<Controller>( () => Controller() )  
149 ``` 111 ```
150 112
151 ### Get.putAsync 113 ### Get.putAsync
  114 +If you want to register an asynchronous instance, you can use `Get.putAsync`:
  115 +
  116 +```dart
  117 +Get.putAsync<SharedPreferences>(() async {
  118 + final prefs = await SharedPreferences.getInstance();
  119 + await prefs.setInt('counter', 12345);
  120 + return prefs;
  121 +});
152 122
153 -Since `Get.put()` does not support async methods/classes, you need to use Get.putAsync. The way of declare is equal to Get.lazyPut 123 +Get.putAsync<YourAsyncClass>( () async => await YourAsyncClass() )
  124 +```
154 125
  126 +This is all options you can set when using putAsync:
155 ```dart 127 ```dart
156 Get.putAsync<S>( 128 Get.putAsync<S>(
157 129
@@ -166,9 +138,6 @@ Get.putAsync<S>( @@ -166,9 +138,6 @@ Get.putAsync<S>(
166 // defaults to false 138 // defaults to false
167 bool permanent = false 139 bool permanent = false
168 ) 140 )
169 -  
170 -// Example  
171 -Get.putAsync<YourAsyncClass>( () async => await YourAsyncClass() )  
172 ``` 141 ```
173 142
174 ### Get.create 143 ### Get.create
@@ -176,6 +145,13 @@ Get.putAsync<YourAsyncClass>( () async => await YourAsyncClass() ) @@ -176,6 +145,13 @@ Get.putAsync<YourAsyncClass>( () async => await YourAsyncClass() )
176 This one is tricky. A detailed explanation of what this is and the differences between the other one can be found on [Differences between methods:](#differences-between-methods) section 145 This one is tricky. A detailed explanation of what this is and the differences between the other one can be found on [Differences between methods:](#differences-between-methods) section
177 146
178 ```dart 147 ```dart
  148 +Get.Create<SomeClass>(() => SomeClass());
  149 +Get.Create<LoginController>(() => LoginController());
  150 +```
  151 +
  152 +This is all options you can set when using create:
  153 +
  154 +```dart
179 Get.create<S>( 155 Get.create<S>(
180 // required: a function that returns a class that will be "fabricated" every 156 // required: a function that returns a class that will be "fabricated" every
181 // time `Get.find()` is called 157 // time `Get.find()` is called
@@ -194,14 +170,48 @@ Get.create<S>( @@ -194,14 +170,48 @@ Get.create<S>(
194 bool permanent = true 170 bool permanent = true
195 ``` 171 ```
196 172
  173 +## Using instantiated methods/classes
  174 +
  175 +Imagine that you have navigated through numerous routes, and you need a data that was left behind in your controller, you would need a state manager combined with the Provider or Get_it, correct? Not with Get. You just need to ask Get to "find" for your controller, you don't need any additional dependencies:
  176 +
  177 +```dart
  178 +final controller = Get.find<Controller>();
  179 +// OR
  180 +Controller controller = Get.find();
  181 +
  182 +// Yes, it looks like Magic, Get will find your controller, and will deliver it to you.
  183 +// You can have 1 million controllers instantiated, Get will always give you the right controller.
  184 +```
  185 +
  186 +And then you will be able to recover your controller data that was obtained back there:
  187 +
  188 +```dart
  189 +Text(controller.textFromApi);
  190 +```
  191 +
  192 +Since the returnd value is a normal class, you can do anything you want:
  193 +```dart
  194 +int count = Get.find<SharedPreferences>().getInt('counter');
  195 +print(count); // out: 12345
  196 +```
  197 +
  198 +To remove a instance of Get:
  199 +
  200 +```dart
  201 +Get.delete<Controller>(); //usually you don't need to do this because GetX already delete unused controllers
  202 +```
  203 +
197 ## Differences between methods 204 ## Differences between methods
198 205
199 First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods. 206 First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods.
200 207
201 The fundamental difference between `permanent` and `fenix` is how you want to store your instances. 208 The fundamental difference between `permanent` and `fenix` is how you want to store your instances.
  209 +
202 Reinforcing: by default, GetX deletes instances when they are not is use. 210 Reinforcing: by default, GetX deletes instances when they are not is use.
203 It means that: If screen 1 has controller 1 and screen 2 has controller 2 and you remove the first route from stack, (like if you use `Get.off()` or `Get.offName()`) the controller 1 lost it's use so it will be erased. 211 It means that: If screen 1 has controller 1 and screen 2 has controller 2 and you remove the first route from stack, (like if you use `Get.off()` or `Get.offName()`) the controller 1 lost it's use so it will be erased.
  212 +
204 But if you want to opt for using `permanent:true`, then the controller will not be lost in this transition - which is very useful for services that you want to keep alive throughout the entire application. 213 But if you want to opt for using `permanent:true`, then the controller will not be lost in this transition - which is very useful for services that you want to keep alive throughout the entire application.
  214 +
205 `fenix` in the other hand is for services that you don't worry in losing between screen changes, but when you need that service, you expect that it is alive. So basically, it will dispose the unused controller/service/class, but when you need it, it will "recreate from the ashes" a new instance. 215 `fenix` in the other hand is for services that you don't worry in losing between screen changes, but when you need that service, you expect that it is alive. So basically, it will dispose the unused controller/service/class, but when you need it, it will "recreate from the ashes" a new instance.
206 216
207 Proceeding with the differences between methods: 217 Proceeding with the differences between methods:
@@ -222,7 +232,7 @@ The Binding class is a class that will decouple dependency injection, while "bin @@ -222,7 +232,7 @@ The Binding class is a class that will decouple dependency injection, while "bin
222 This allows Get to know which screen is being displayed when a particular controller is used and to know where and how to dispose of it. 232 This allows Get to know which screen is being displayed when a particular controller is used and to know where and how to dispose of it.
223 In addition, the Binding class will allow you to have SmartManager configuration control. You can configure the dependencies to be arranged when removing a route from the stack, or when the widget that used it is laid out, or neither. You will have intelligent dependency management working for you, but even so, you can configure it as you wish. 233 In addition, the Binding class will allow you to have SmartManager configuration control. You can configure the dependencies to be arranged when removing a route from the stack, or when the widget that used it is laid out, or neither. You will have intelligent dependency management working for you, but even so, you can configure it as you wish.
224 234
225 -### How to use 235 +### Bindings class
226 236
227 - Create a class and implements Binding 237 - Create a class and implements Binding
228 238
@@ -323,6 +333,23 @@ GetX by default disposes unused controllers from memory, even if a failure occur @@ -323,6 +333,23 @@ GetX by default disposes unused controllers from memory, even if a failure occur
323 This is what is called the `full` mode of dependency management. 333 This is what is called the `full` mode of dependency management.
324 But if you want to change the way GetX controls the disposal of classes, you have `SmartManagement` class that you can set different behaviors. 334 But if you want to change the way GetX controls the disposal of classes, you have `SmartManagement` class that you can set different behaviors.
325 335
  336 +#### How to change
  337 +
  338 +If you want to change this config (which you usually don't need) this is the way:
  339 +
  340 +```dart
  341 +void main () {
  342 + runApp(
  343 + GetMaterialApp(
  344 + smartManagement: SmartManagement.onlyBuilders //here
  345 + home: Home(),
  346 + )
  347 + )
  348 +}
  349 +
  350 +
  351 +```
  352 +
326 #### SmartManagement.full 353 #### SmartManagement.full
327 354
328 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. 355 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.