Showing
1 changed file
with
64 additions
and
62 deletions
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,59 +26,15 @@ Controller controller = Get.put(Controller()); // Rather Controller controller = | @@ -26,59 +26,15 @@ 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 | ||
63 | - | ||
64 | -```dart | ||
65 | -int count = Get.find<SharedPreferences>().getInt('counter'); | ||
66 | -print(count); // out: 12345 | ||
67 | -``` | ||
68 | - | ||
69 | -To remove a instance of Get: | ||
70 | - | ||
71 | -```dart | ||
72 | -Get.delete<Controller>(); | ||
73 | -``` | ||
74 | - | ||
75 | ## Instancing methods | 32 | ## 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: | 33 | +The methods and it's configurable parameters are: |
78 | 34 | ||
79 | ### Get.put() | 35 | ### Get.put() |
80 | 36 | ||
81 | -The most common way of inserting a dependency. Good for the controllers of your views for example. | 37 | +The most common way of insert a dependency. Good for the controllers of your views for example. |
82 | 38 | ||
83 | ```dart | 39 | ```dart |
84 | Get.put<S>( | 40 | Get.put<S>( |
@@ -114,9 +70,25 @@ Get.put<LoginController>(LoginController(), permanent: true) | @@ -114,9 +70,25 @@ Get.put<LoginController>(LoginController(), permanent: true) | ||
114 | ``` | 70 | ``` |
115 | 71 | ||
116 | ### Get.lazyPut | 72 | ### Get.lazyPut |
73 | +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. | ||
74 | + | ||
75 | +```dart | ||
76 | +/// ApiMock will only be called when someone uses Get.find<ApiMock> for the first time | ||
77 | +Get.lazyPut<ApiMock>(() => ApiMock()); | ||
78 | + | ||
79 | +Get.lazyPut<FirebaseAuth>( | ||
80 | + () => { | ||
81 | + // ... some logic if needed | ||
82 | + return FirebaseAuth() | ||
83 | + }, | ||
84 | + tag: Math.random().toString(), | ||
85 | + fenix: true | ||
86 | +) | ||
117 | 87 | ||
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 | 88 | +Get.lazyPut<Controller>( () => Controller() ) |
89 | +``` | ||
119 | 90 | ||
91 | +This is all options you can set when using lazyPut: | ||
120 | ```dart | 92 | ```dart |
121 | Get.lazyPut<S>( | 93 | Get.lazyPut<S>( |
122 | // mandatory: a method that will be executed when your class is called for the first time | 94 | // mandatory: a method that will be executed when your class is called for the first time |
@@ -133,26 +105,23 @@ Get.lazyPut<S>( | @@ -133,26 +105,23 @@ Get.lazyPut<S>( | ||
133 | bool fenix = false | 105 | bool fenix = false |
134 | 106 | ||
135 | ) | 107 | ) |
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 | ``` | 108 | ``` |
150 | 109 | ||
151 | ### Get.putAsync | 110 | ### Get.putAsync |
152 | 111 | ||
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 | 112 | Since `Get.put()` does not support async methods/classes, you need to use Get.putAsync. The way of declare is equal to Get.lazyPut |
113 | +If you want to register an asynchronous instance, you can use `Get.putAsync`: | ||
154 | 114 | ||
155 | ```dart | 115 | ```dart |
116 | +Get.putAsync<SharedPreferences>(() async { | ||
117 | + final prefs = await SharedPreferences.getInstance(); | ||
118 | + await prefs.setInt('counter', 12345); | ||
119 | + return prefs; | ||
120 | +}); | ||
121 | +``` | ||
122 | + | ||
123 | +This is all options you can set when using putAsync: | ||
124 | +```dart | ||
156 | Get.putAsync<S>( | 125 | Get.putAsync<S>( |
157 | 126 | ||
158 | // mandatory: an async method that will be executed to instantiate your class | 127 | // mandatory: an async method that will be executed to instantiate your class |
@@ -194,14 +163,47 @@ Get.create<S>( | @@ -194,14 +163,47 @@ Get.create<S>( | ||
194 | bool permanent = true | 163 | bool permanent = true |
195 | ``` | 164 | ``` |
196 | 165 | ||
166 | +## Using instantiated methods/classes | ||
167 | + | ||
168 | +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: | ||
169 | + | ||
170 | +```dart | ||
171 | +final controller = Get.find<Controller>(); | ||
172 | +// OR | ||
173 | +Controller controller = Get.find(); | ||
174 | + | ||
175 | +//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. | ||
176 | +``` | ||
177 | + | ||
178 | +And then you will be able to recover your controller data that was obtained back there: | ||
179 | + | ||
180 | +```dart | ||
181 | +Text(controller.textFromApi); | ||
182 | +``` | ||
183 | + | ||
184 | +Since the returnd value is a normal class, you can do anything you want: | ||
185 | +```dart | ||
186 | +int count = Get.find<SharedPreferences>().getInt('counter'); | ||
187 | +print(count); // out: 12345 | ||
188 | +``` | ||
189 | + | ||
190 | +To remove a instance of Get: | ||
191 | + | ||
192 | +```dart | ||
193 | +Get.delete<Controller>(); //usually you don't need to do this because GetX already delete unused controllers | ||
194 | +``` | ||
195 | + | ||
197 | ## Differences between methods | 196 | ## Differences between methods |
198 | 197 | ||
199 | First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods. | 198 | First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods. |
200 | 199 | ||
201 | The fundamental difference between `permanent` and `fenix` is how you want to store your instances. | 200 | The fundamental difference between `permanent` and `fenix` is how you want to store your instances. |
201 | + | ||
202 | Reinforcing: by default, GetX deletes instances when they are not is use. | 202 | 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. | 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. |
204 | + | ||
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. | 205 | 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. |
206 | + | ||
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. | 207 | `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 | 208 | ||
207 | Proceeding with the differences between methods: | 209 | Proceeding with the differences between methods: |
-
Please register or login to post a comment