Fixes Get.create() and GetWidget()
- Although "nasty" solution, multiple instances were created with one Get.find() when using Get.create(), now the actual instance created from `GetInstance::<S>_startController()` > `GetInstance::<S>_initDependencies()` > `GetInstance::<S>_find()` is returned. - added docs and impl to `GetWidget` to make it work with **DI** `tag`, I place it in the constructor directly (to use with `:super( tag: "something")` )... but we might actually use an override getter as well if needed.
Showing
2 changed files
with
22 additions
and
9 deletions
1 | import 'dart:async'; | 1 | import 'dart:async'; |
2 | import 'dart:collection'; | 2 | import 'dart:collection'; |
3 | + | ||
3 | import 'package:get_core/get_core.dart'; | 4 | import 'package:get_core/get_core.dart'; |
5 | + | ||
4 | import 'lifecircle.dart'; | 6 | import 'lifecircle.dart'; |
5 | 7 | ||
6 | class GetInstance { | 8 | class GetInstance { |
@@ -178,11 +180,14 @@ class GetInstance { | @@ -178,11 +180,14 @@ class GetInstance { | ||
178 | /// [Get.keepFactory] | 180 | /// [Get.keepFactory] |
179 | /// Only flags `isInit` if it's using `Get.create()` | 181 | /// Only flags `isInit` if it's using `Get.create()` |
180 | /// (not for Singletons access). | 182 | /// (not for Singletons access). |
181 | - bool _initDependencies<S>({String name}) { | 183 | + /// Returns the instance if not initialized, required for Get.create() to |
184 | + /// work properly. | ||
185 | + S _initDependencies<S>({String name}) { | ||
182 | final key = _getKey(S, name); | 186 | final key = _getKey(S, name); |
183 | final isInit = _singl[key].isInit; | 187 | final isInit = _singl[key].isInit; |
188 | + S i; | ||
184 | if (!isInit) { | 189 | if (!isInit) { |
185 | - _startController<S>(tag: name); | 190 | + i = _startController<S>(tag: name); |
186 | if (_singl[key].isSingleton) { | 191 | if (_singl[key].isSingleton) { |
187 | _singl[key].isInit = true; | 192 | _singl[key].isInit = true; |
188 | if (Get.smartManagement != SmartManagement.onlyBuilder) { | 193 | if (Get.smartManagement != SmartManagement.onlyBuilder) { |
@@ -190,7 +195,7 @@ class GetInstance { | @@ -190,7 +195,7 @@ class GetInstance { | ||
190 | } | 195 | } |
191 | } | 196 | } |
192 | } | 197 | } |
193 | - return true; | 198 | + return i; |
194 | } | 199 | } |
195 | 200 | ||
196 | /// Links a Class instance [S] (or [tag]) to the current route. | 201 | /// Links a Class instance [S] (or [tag]) to the current route. |
@@ -206,9 +211,9 @@ class GetInstance { | @@ -206,9 +211,9 @@ class GetInstance { | ||
206 | } | 211 | } |
207 | 212 | ||
208 | /// Initializes the controller | 213 | /// Initializes the controller |
209 | - void _startController<S>({String tag}) { | 214 | + S _startController<S>({String tag}) { |
210 | final key = _getKey(S, tag); | 215 | final key = _getKey(S, tag); |
211 | - final i = _singl[key].getDependency(); | 216 | + final i = _singl[key].getDependency() as S; |
212 | if (i is GetLifeCycle) { | 217 | if (i is GetLifeCycle) { |
213 | if (i.onStart != null) { | 218 | if (i.onStart != null) { |
214 | i.onStart(); | 219 | i.onStart(); |
@@ -219,6 +224,7 @@ class GetInstance { | @@ -219,6 +224,7 @@ class GetInstance { | ||
219 | _routesByCreate[Get.reference].add(i.onClose); | 224 | _routesByCreate[Get.reference].add(i.onClose); |
220 | } | 225 | } |
221 | } | 226 | } |
227 | + return i; | ||
222 | } | 228 | } |
223 | 229 | ||
224 | // S putOrFind<S>(S Function() dep, {String tag}) { | 230 | // S putOrFind<S>(S Function() dep, {String tag}) { |
@@ -257,8 +263,12 @@ class GetInstance { | @@ -257,8 +263,12 @@ class GetInstance { | ||
257 | throw 'Class "$S" with tag "$tag" is not register'; | 263 | throw 'Class "$S" with tag "$tag" is not register'; |
258 | } | 264 | } |
259 | } | 265 | } |
260 | - _initDependencies<S>(name: tag); | ||
261 | - return _singl[key].getDependency() as S; | 266 | + |
267 | + /// although dirty solution, the lifecycle starts inside | ||
268 | + /// `initDependencies`, so we have to return the instance from there | ||
269 | + /// to make it compatible with `Get.create()`. | ||
270 | + final i = _initDependencies<S>(name: tag); | ||
271 | + return i ?? _singl[key].getDependency() as S; | ||
262 | } else { | 272 | } else { |
263 | if (!_factory.containsKey(key)) { | 273 | if (!_factory.containsKey(key)) { |
264 | // ignore: lines_longer_than_80_chars | 274 | // ignore: lines_longer_than_80_chars |
@@ -11,6 +11,9 @@ import 'package:get_instance/get_instance.dart'; | @@ -11,6 +11,9 @@ import 'package:get_instance/get_instance.dart'; | ||
11 | /// } | 11 | /// } |
12 | /// | 12 | /// |
13 | /// class AwesomeView extends GetView<AwesomeController> { | 13 | /// class AwesomeView extends GetView<AwesomeController> { |
14 | +/// /// if you need you can pass the tag for your controller | ||
15 | +/// /// in the super constructor. | ||
16 | +/// AwesomeView({Key key}):super(key:key, tag:"myControllerTag"); | ||
14 | /// @override | 17 | /// @override |
15 | /// Widget build(BuildContext context) { | 18 | /// Widget build(BuildContext context) { |
16 | /// return Container( | 19 | /// return Container( |
@@ -21,9 +24,9 @@ import 'package:get_instance/get_instance.dart'; | @@ -21,9 +24,9 @@ import 'package:get_instance/get_instance.dart'; | ||
21 | /// } | 24 | /// } |
22 | ///`` | 25 | ///`` |
23 | abstract class GetView<T> extends StatelessWidget { | 26 | abstract class GetView<T> extends StatelessWidget { |
24 | - const GetView({Key key}) : super(key: key); | 27 | + const GetView({Key key, this.tag}) : super(key: key); |
25 | 28 | ||
26 | - final String tag = null; | 29 | + final String tag; |
27 | 30 | ||
28 | T get controller => GetInstance().find<T>(tag: tag); | 31 | T get controller => GetInstance().find<T>(tag: tag); |
29 | 32 |
-
Please register or login to post a comment