Committed by
GitHub
Merge pull request #647 from roipeker/fix_getwidget
Fixes Get.create() and GetWidget()
Showing
3 changed files
with
30 additions
and
11 deletions
1 | import 'package:get_core/get_core.dart'; | 1 | import 'package:get_core/get_core.dart'; |
2 | + | ||
2 | import 'get_instance.dart'; | 3 | import 'get_instance.dart'; |
3 | 4 | ||
4 | extension Inst on GetInterface { | 5 | extension Inst on GetInterface { |
@@ -47,6 +48,7 @@ extension Inst on GetInterface { | @@ -47,6 +48,7 @@ extension Inst on GetInterface { | ||
47 | /// So, if you call `Get.delete<T>()` the "instance factory" used in this | 48 | /// So, if you call `Get.delete<T>()` the "instance factory" used in this |
48 | /// method ([Get.create<T>()]) will be removed, but NOT the instances | 49 | /// method ([Get.create<T>()]) will be removed, but NOT the instances |
49 | /// already created by it. | 50 | /// already created by it. |
51 | + /// Uses `tag` as the other methods. | ||
50 | /// | 52 | /// |
51 | /// Example: | 53 | /// Example: |
52 | /// | 54 | /// |
@@ -55,8 +57,8 @@ extension Inst on GetInterface { | @@ -55,8 +57,8 @@ extension Inst on GetInterface { | ||
55 | /// Repl b = find(); | 57 | /// Repl b = find(); |
56 | /// print(a==b); (false)``` | 58 | /// print(a==b); (false)``` |
57 | void create<S>(InstanceBuilderCallback<S> builder, | 59 | void create<S>(InstanceBuilderCallback<S> builder, |
58 | - {String name, bool permanent = true}) => | ||
59 | - GetInstance().create<S>(builder, name: name, permanent: permanent); | 60 | + {String tag, bool permanent = true}) => |
61 | + GetInstance().create<S>(builder, tag: tag, permanent: permanent); | ||
60 | 62 | ||
61 | /// Finds a Instance of the required Class <[S]>(or [tag]) | 63 | /// Finds a Instance of the required Class <[S]>(or [tag]) |
62 | /// In the case of using [Get.create()], it will generate an Instance | 64 | /// In the case of using [Get.create()], it will generate an Instance |
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 { |
@@ -114,11 +116,11 @@ class GetInstance { | @@ -114,11 +116,11 @@ class GetInstance { | ||
114 | /// print(a==b); (false)``` | 116 | /// print(a==b); (false)``` |
115 | void create<S>( | 117 | void create<S>( |
116 | InstanceBuilderCallback<S> builder, { | 118 | InstanceBuilderCallback<S> builder, { |
117 | - String name, | 119 | + String tag, |
118 | bool permanent = true, | 120 | bool permanent = true, |
119 | }) { | 121 | }) { |
120 | _insert( | 122 | _insert( |
121 | - isSingleton: false, name: name, builder: builder, permanent: permanent); | 123 | + isSingleton: false, name: tag, builder: builder, permanent: permanent); |
122 | } | 124 | } |
123 | 125 | ||
124 | /// Injects the Instance [S] builder into the [_singleton] HashMap. | 126 | /// Injects the Instance [S] builder into the [_singleton] HashMap. |
@@ -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,13 @@ import 'package:get_instance/get_instance.dart'; | @@ -11,6 +11,13 @@ 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 | ||
15 | +/// /// Get.find<AwesomeController>(tag:"myTag"); | ||
16 | +/// @override | ||
17 | +/// final String tag = "myTag"; | ||
18 | +/// | ||
19 | +/// AwesomeView({Key key}):super(key:key); | ||
20 | +/// | ||
14 | /// @override | 21 | /// @override |
15 | /// Widget build(BuildContext context) { | 22 | /// Widget build(BuildContext context) { |
16 | /// return Container( | 23 | /// return Container( |
-
Please register or login to post a comment