Committed by
GitHub
Merge pull request #1164 from eduardoflorence/null-safety-put-not-null
Get.put/putAsync/find not nullable
Showing
8 changed files
with
35 additions
and
31 deletions
@@ -6,8 +6,8 @@ import 'package:get/get.dart'; | @@ -6,8 +6,8 @@ import 'package:get/get.dart'; | ||
6 | import 'package:get_demo/pages/home/domain/adapters/repository_adapter.dart'; | 6 | import 'package:get_demo/pages/home/domain/adapters/repository_adapter.dart'; |
7 | import 'package:get_demo/pages/home/domain/entity/cases_model.dart'; | 7 | import 'package:get_demo/pages/home/domain/entity/cases_model.dart'; |
8 | import 'package:get_demo/pages/home/presentation/controllers/home_controller.dart'; | 8 | import 'package:get_demo/pages/home/presentation/controllers/home_controller.dart'; |
9 | -import 'package:get_demo/routes/app_pages.dart'; | ||
10 | -import 'package:get_test/get_test.dart'; | 9 | +// import 'package:get_demo/routes/app_pages.dart'; |
10 | +// import 'package:get_test/get_test.dart'; | ||
11 | import 'package:matcher/matcher.dart' as m; | 11 | import 'package:matcher/matcher.dart' as m; |
12 | 12 | ||
13 | class MockRepository implements IHomeRepository { | 13 | class MockRepository implements IHomeRepository { |
@@ -37,11 +37,12 @@ class MockRepository implements IHomeRepository { | @@ -37,11 +37,12 @@ class MockRepository implements IHomeRepository { | ||
37 | } | 37 | } |
38 | 38 | ||
39 | void main() { | 39 | void main() { |
40 | + WidgetsFlutterBinding.ensureInitialized(); | ||
40 | setUpAll(() => HttpOverrides.global = null); | 41 | setUpAll(() => HttpOverrides.global = null); |
41 | final binding = BindingsBuilder(() { | 42 | final binding = BindingsBuilder(() { |
42 | Get.lazyPut<IHomeRepository>(() => MockRepository()); | 43 | Get.lazyPut<IHomeRepository>(() => MockRepository()); |
43 | Get.lazyPut<HomeController>( | 44 | Get.lazyPut<HomeController>( |
44 | - () => HomeController(homeRepository: Get.find()!)); | 45 | + () => HomeController(homeRepository: Get.find())); |
45 | }); | 46 | }); |
46 | 47 | ||
47 | test('Test Binding', () { | 48 | test('Test Binding', () { |
@@ -86,7 +87,9 @@ void main() { | @@ -86,7 +87,9 @@ void main() { | ||
86 | }); | 87 | }); |
87 | 88 | ||
88 | /// Tests with GetTests | 89 | /// Tests with GetTests |
89 | - getTest( | 90 | + /// TEMPORARILY REMOVED from the null-safetym branch as |
91 | + /// get_test is not yet null safety. | ||
92 | + /* getTest( | ||
90 | "test description", | 93 | "test description", |
91 | getPages: AppPages.routes, | 94 | getPages: AppPages.routes, |
92 | initialRoute: AppPages.INITIAL, | 95 | initialRoute: AppPages.INITIAL, |
@@ -135,7 +138,7 @@ void main() { | @@ -135,7 +138,7 @@ void main() { | ||
135 | onClose: (c) { | 138 | onClose: (c) { |
136 | print('onClose'); | 139 | print('onClose'); |
137 | }, | 140 | }, |
138 | - ); | 141 | + );*/ |
139 | } | 142 | } |
140 | 143 | ||
141 | class Controller extends GetxController { | 144 | class Controller extends GetxController { |
@@ -39,7 +39,7 @@ extension Inst on GetInterface { | @@ -39,7 +39,7 @@ extension Inst on GetInterface { | ||
39 | /// async version of [Get.put()]. | 39 | /// async version of [Get.put()]. |
40 | /// Awaits for the resolution of the Future from [builder()] parameter and | 40 | /// Awaits for the resolution of the Future from [builder()] parameter and |
41 | /// stores the Instance returned. | 41 | /// stores the Instance returned. |
42 | - Future<S?> putAsync<S>(AsyncInstanceBuilderCallback<S> builder, | 42 | + Future<S> putAsync<S>(AsyncInstanceBuilderCallback<S> builder, |
43 | {String? tag, bool permanent = false}) async => | 43 | {String? tag, bool permanent = false}) async => |
44 | GetInstance().putAsync<S>(builder, tag: tag, permanent: permanent); | 44 | GetInstance().putAsync<S>(builder, tag: tag, permanent: permanent); |
45 | 45 | ||
@@ -67,7 +67,7 @@ extension Inst on GetInterface { | @@ -67,7 +67,7 @@ extension Inst on GetInterface { | ||
67 | /// Finds a Instance of the required Class <[S]>(or [tag]) | 67 | /// Finds a Instance of the required Class <[S]>(or [tag]) |
68 | /// In the case of using [Get.create()], it will generate an Instance | 68 | /// In the case of using [Get.create()], it will generate an Instance |
69 | /// each time you call [Get.find()]. | 69 | /// each time you call [Get.find()]. |
70 | - S find<S>({String? tag}) => GetInstance().find<S>(tag: tag)!; | 70 | + S find<S>({String? tag}) => GetInstance().find<S>(tag: tag); |
71 | 71 | ||
72 | /// Injects an [Instance<S>] in memory. | 72 | /// Injects an [Instance<S>] in memory. |
73 | /// | 73 | /// |
@@ -83,7 +83,7 @@ extension Inst on GetInterface { | @@ -83,7 +83,7 @@ extension Inst on GetInterface { | ||
83 | /// rules. Although, can be removed by [GetInstance.reset()] | 83 | /// rules. Although, can be removed by [GetInstance.reset()] |
84 | /// and [Get.delete()] | 84 | /// and [Get.delete()] |
85 | /// - [builder] If defined, the [dependency] must be returned from here | 85 | /// - [builder] If defined, the [dependency] must be returned from here |
86 | - S? put<S>(S dependency, | 86 | + S put<S>(S dependency, |
87 | {String? tag, | 87 | {String? tag, |
88 | bool permanent = false, | 88 | bool permanent = false, |
89 | InstanceBuilderCallback<S>? builder}) => | 89 | InstanceBuilderCallback<S>? builder}) => |
@@ -28,7 +28,7 @@ class GetInstance { | @@ -28,7 +28,7 @@ class GetInstance { | ||
28 | 28 | ||
29 | static GetInstance? _getInstance; | 29 | static GetInstance? _getInstance; |
30 | 30 | ||
31 | - T? call<T>() => find<T>(); | 31 | + T call<T>() => find<T>(); |
32 | 32 | ||
33 | /// Holds references to every registered Instance when using | 33 | /// Holds references to every registered Instance when using |
34 | /// [Get.put()] | 34 | /// [Get.put()] |
@@ -69,7 +69,7 @@ class GetInstance { | @@ -69,7 +69,7 @@ class GetInstance { | ||
69 | /// async version of [Get.put()]. | 69 | /// async version of [Get.put()]. |
70 | /// Awaits for the resolution of the Future from [builder()] parameter and | 70 | /// Awaits for the resolution of the Future from [builder()] parameter and |
71 | /// stores the Instance returned. | 71 | /// stores the Instance returned. |
72 | - Future<S?> putAsync<S>( | 72 | + Future<S> putAsync<S>( |
73 | AsyncInstanceBuilderCallback<S> builder, { | 73 | AsyncInstanceBuilderCallback<S> builder, { |
74 | String? tag, | 74 | String? tag, |
75 | bool permanent = false, | 75 | bool permanent = false, |
@@ -87,7 +87,7 @@ class GetInstance { | @@ -87,7 +87,7 @@ class GetInstance { | ||
87 | /// the same Type<[S]> | 87 | /// the same Type<[S]> |
88 | /// - [permanent] keeps the Instance in memory, not following | 88 | /// - [permanent] keeps the Instance in memory, not following |
89 | /// [Get.smartManagement] rules. | 89 | /// [Get.smartManagement] rules. |
90 | - S? put<S>( | 90 | + S put<S>( |
91 | S dependency, { | 91 | S dependency, { |
92 | String? tag, | 92 | String? tag, |
93 | bool permanent = false, | 93 | bool permanent = false, |
@@ -277,9 +277,9 @@ class GetInstance { | @@ -277,9 +277,9 @@ class GetInstance { | ||
277 | } | 277 | } |
278 | 278 | ||
279 | /// Initializes the controller | 279 | /// Initializes the controller |
280 | - S? _startController<S>({String? tag}) { | 280 | + S _startController<S>({String? tag}) { |
281 | final key = _getKey(S, tag); | 281 | final key = _getKey(S, tag); |
282 | - final i = _singl[key]!.getDependency() as S?; | 282 | + final i = _singl[key]!.getDependency() as S; |
283 | if (i is GetLifeCycleBase) { | 283 | if (i is GetLifeCycleBase) { |
284 | i.onStart(); | 284 | i.onStart(); |
285 | if (tag == null) { | 285 | if (tag == null) { |
@@ -289,17 +289,18 @@ class GetInstance { | @@ -289,17 +289,18 @@ class GetInstance { | ||
289 | } | 289 | } |
290 | if (!_singl[key]!.isSingleton!) { | 290 | if (!_singl[key]!.isSingleton!) { |
291 | _routesByCreate[Get.reference] ??= HashSet<Function>(); | 291 | _routesByCreate[Get.reference] ??= HashSet<Function>(); |
292 | - _routesByCreate[Get.reference]!.add(i.onDelete as Function); | 292 | + // _routesByCreate[Get.reference]!.add(i.onDelete as Function); |
293 | + _routesByCreate[Get.reference]!.add(i.onDelete); | ||
293 | } | 294 | } |
294 | } | 295 | } |
295 | return i; | 296 | return i; |
296 | } | 297 | } |
297 | 298 | ||
298 | - S? putOrFind<S>(InstanceBuilderCallback<S> dep, {String? tag}) { | 299 | + S putOrFind<S>(InstanceBuilderCallback<S> dep, {String? tag}) { |
299 | final key = _getKey(S, tag); | 300 | final key = _getKey(S, tag); |
300 | 301 | ||
301 | if (_singl.containsKey(key)) { | 302 | if (_singl.containsKey(key)) { |
302 | - return _singl[key]!.getDependency() as S?; | 303 | + return _singl[key]!.getDependency() as S; |
303 | } else { | 304 | } else { |
304 | return GetInstance().put(dep(), tag: tag); | 305 | return GetInstance().put(dep(), tag: tag); |
305 | } | 306 | } |
@@ -310,7 +311,7 @@ class GetInstance { | @@ -310,7 +311,7 @@ class GetInstance { | ||
310 | /// it will create an instance each time you call [find]. | 311 | /// it will create an instance each time you call [find]. |
311 | /// If the registered type <[S]> (or [tag]) is a Controller, | 312 | /// If the registered type <[S]> (or [tag]) is a Controller, |
312 | /// it will initialize it's lifecycle. | 313 | /// it will initialize it's lifecycle. |
313 | - S? find<S>({String? tag}) { | 314 | + S find<S>({String? tag}) { |
314 | final key = _getKey(S, tag); | 315 | final key = _getKey(S, tag); |
315 | if (isRegistered<S>(tag: tag)) { | 316 | if (isRegistered<S>(tag: tag)) { |
316 | if (_singl[key] == null) { | 317 | if (_singl[key] == null) { |
@@ -325,7 +326,7 @@ class GetInstance { | @@ -325,7 +326,7 @@ class GetInstance { | ||
325 | /// `initDependencies`, so we have to return the instance from there | 326 | /// `initDependencies`, so we have to return the instance from there |
326 | /// to make it compatible with `Get.create()`. | 327 | /// to make it compatible with `Get.create()`. |
327 | final i = _initDependencies<S>(name: tag); | 328 | final i = _initDependencies<S>(name: tag); |
328 | - return i ?? _singl[key]!.getDependency() as S?; | 329 | + return i ?? _singl[key]!.getDependency() as S; |
329 | } else { | 330 | } else { |
330 | // ignore: lines_longer_than_80_chars | 331 | // ignore: lines_longer_than_80_chars |
331 | throw '"$S" not found. You need to call "Get.put($S())" or "Get.lazyPut(()=>$S())"'; | 332 | throw '"$S" not found. You need to call "Get.put($S())" or "Get.lazyPut(()=>$S())"'; |
@@ -513,13 +514,13 @@ class _InstanceBuilderFactory<S> { | @@ -513,13 +514,13 @@ class _InstanceBuilderFactory<S> { | ||
513 | } | 514 | } |
514 | 515 | ||
515 | /// Gets the actual instance by it's [builderFunc] or the persisted instance. | 516 | /// Gets the actual instance by it's [builderFunc] or the persisted instance. |
516 | - S? getDependency() { | 517 | + S getDependency() { |
517 | if (isSingleton!) { | 518 | if (isSingleton!) { |
518 | if (dependency == null) { | 519 | if (dependency == null) { |
519 | _showInitLog(); | 520 | _showInitLog(); |
520 | dependency = builderFunc(); | 521 | dependency = builderFunc(); |
521 | } | 522 | } |
522 | - return dependency; | 523 | + return dependency!; |
523 | } else { | 524 | } else { |
524 | return builderFunc(); | 525 | return builderFunc(); |
525 | } | 526 | } |
@@ -136,7 +136,7 @@ void main() { | @@ -136,7 +136,7 @@ void main() { | ||
136 | tearDownAll(Get.reset); | 136 | tearDownAll(Get.reset); |
137 | 137 | ||
138 | test('Get.put test with init check', () async { | 138 | test('Get.put test with init check', () async { |
139 | - final instance = Get.put(DisposableController())!; | 139 | + final instance = Get.put(DisposableController()); |
140 | expect(instance, Get.find<DisposableController>()); | 140 | expect(instance, Get.find<DisposableController>()); |
141 | expect(instance.initialized, true); | 141 | expect(instance.initialized, true); |
142 | }); | 142 | }); |
@@ -150,7 +150,7 @@ void main() { | @@ -150,7 +150,7 @@ void main() { | ||
150 | 150 | ||
151 | test('Get.put test after delete with disposable controller and init check', | 151 | test('Get.put test after delete with disposable controller and init check', |
152 | () async { | 152 | () async { |
153 | - final instance = Get.put<DisposableController>(DisposableController())!; | 153 | + final instance = Get.put<DisposableController>(DisposableController()); |
154 | expect(instance, Get.find<DisposableController>()); | 154 | expect(instance, Get.find<DisposableController>()); |
155 | expect(instance.initialized, true); | 155 | expect(instance.initialized, true); |
156 | }); | 156 | }); |
@@ -51,7 +51,7 @@ void main() { | @@ -51,7 +51,7 @@ void main() { | ||
51 | expect(find.text("List: 0"), findsOneWidget); | 51 | expect(find.text("List: 0"), findsOneWidget); |
52 | expect(find.text("Map: 0"), findsOneWidget); | 52 | expect(find.text("Map: 0"), findsOneWidget); |
53 | 53 | ||
54 | - Controller.to!.increment(); | 54 | + Controller.to.increment(); |
55 | 55 | ||
56 | await tester.pump(); | 56 | await tester.pump(); |
57 | 57 | ||
@@ -79,7 +79,7 @@ void main() { | @@ -79,7 +79,7 @@ void main() { | ||
79 | } | 79 | } |
80 | 80 | ||
81 | class Controller extends GetxController { | 81 | class Controller extends GetxController { |
82 | - static Controller? get to => Get.find(); | 82 | + static Controller get to => Get.find(); |
83 | int count = 0; | 83 | int count = 0; |
84 | RxInt counter = 0.obs; | 84 | RxInt counter = 0.obs; |
85 | RxDouble doubleNum = 0.0.obs; | 85 | RxDouble doubleNum = 0.0.obs; |
@@ -11,7 +11,7 @@ void main() { | @@ -11,7 +11,7 @@ void main() { | ||
11 | children: [ | 11 | children: [ |
12 | Obx( | 12 | Obx( |
13 | () => Column(children: [ | 13 | () => Column(children: [ |
14 | - Text('Count: ${controller!.counter.value}'), | 14 | + Text('Count: ${controller.counter.value}'), |
15 | Text('Double: ${controller.doubleNum.value}'), | 15 | Text('Double: ${controller.doubleNum.value}'), |
16 | Text('String: ${controller.string.value}'), | 16 | Text('String: ${controller.string.value}'), |
17 | Text('List: ${controller.list.length}'), | 17 | Text('List: ${controller.list.length}'), |
@@ -37,7 +37,7 @@ void main() { | @@ -37,7 +37,7 @@ void main() { | ||
37 | expect(find.text("Map: 0"), findsOneWidget); | 37 | expect(find.text("Map: 0"), findsOneWidget); |
38 | expect(find.text("Obx: 0"), findsOneWidget); | 38 | expect(find.text("Obx: 0"), findsOneWidget); |
39 | 39 | ||
40 | - Controller.to!.increment(); | 40 | + Controller.to.increment(); |
41 | 41 | ||
42 | await tester.pump(); | 42 | await tester.pump(); |
43 | 43 | ||
@@ -52,7 +52,7 @@ void main() { | @@ -52,7 +52,7 @@ void main() { | ||
52 | } | 52 | } |
53 | 53 | ||
54 | class Controller extends GetxController { | 54 | class Controller extends GetxController { |
55 | - static Controller? get to => Get.find(); | 55 | + static Controller get to => Get.find(); |
56 | 56 | ||
57 | RxInt counter = 0.obs; | 57 | RxInt counter = 0.obs; |
58 | RxDouble doubleNum = 0.0.obs; | 58 | RxDouble doubleNum = 0.0.obs; |
@@ -57,7 +57,7 @@ void main() { | @@ -57,7 +57,7 @@ void main() { | ||
57 | expect(find.text("List: 0"), findsOneWidget); | 57 | expect(find.text("List: 0"), findsOneWidget); |
58 | expect(find.text("Map: 0"), findsOneWidget); | 58 | expect(find.text("Map: 0"), findsOneWidget); |
59 | 59 | ||
60 | - Controller.to!.increment(); | 60 | + Controller.to.increment(); |
61 | 61 | ||
62 | await tester.pump(); | 62 | await tester.pump(); |
63 | 63 | ||
@@ -82,7 +82,7 @@ class ControllerNonGlobal extends GetxController { | @@ -82,7 +82,7 @@ class ControllerNonGlobal extends GetxController { | ||
82 | } | 82 | } |
83 | 83 | ||
84 | class Controller extends GetxController { | 84 | class Controller extends GetxController { |
85 | - static Controller? get to => Get.find(); | 85 | + static Controller get to => Get.find(); |
86 | 86 | ||
87 | RxInt counter = 0.obs; | 87 | RxInt counter = 0.obs; |
88 | RxDouble doubleNum = 0.0.obs; | 88 | RxDouble doubleNum = 0.0.obs; |
@@ -47,7 +47,7 @@ void main() { | @@ -47,7 +47,7 @@ void main() { | ||
47 | 47 | ||
48 | expect(find.text("0"), findsOneWidget); | 48 | expect(find.text("0"), findsOneWidget); |
49 | 49 | ||
50 | - Controller.to!.increment(); | 50 | + Controller.to.increment(); |
51 | 51 | ||
52 | await test.pump(); | 52 | await test.pump(); |
53 | 53 | ||
@@ -83,7 +83,7 @@ void main() { | @@ -83,7 +83,7 @@ void main() { | ||
83 | } | 83 | } |
84 | 84 | ||
85 | class Controller extends GetxController { | 85 | class Controller extends GetxController { |
86 | - static Controller? get to => Get.find(); | 86 | + static Controller get to => Get.find(); |
87 | 87 | ||
88 | int counter = 0; | 88 | int counter = 0; |
89 | 89 |
-
Please register or login to post a comment