Jonny Borges
Committed by GitHub

Merge pull request #2837 from aryan-more/master

Added New method Get.findOrNull ()
... ... @@ -298,6 +298,17 @@ extension Inst on GetInterface {
}
}
/// The findOrNull method will return the instance if it is registered;
/// otherwise, it will return null.
S? findOrNull<S>({String? tag}) {
if (isRegistered<S>(tag: tag)) {
return find<S>(tag: tag);
}
return null;
}
/// Replace a parent instance of a class in dependency management
/// with a [child] instance
/// - [tag] optional, if you use a [tag] to register the Instance.
... ...
... ... @@ -55,12 +55,9 @@ void main() {
final instance = Get.put<Controller>(Controller(), tag: 'one');
final instance2 = Get.put<Controller>(Controller(), tag: 'two');
expect(instance == instance2, false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'two'),
false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'one'),
true);
expect(Get.find<Controller>(tag: 'two') == Get.find<Controller>(tag: 'two'),
true);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'two'), false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'one'), true);
expect(Get.find<Controller>(tag: 'two') == Get.find<Controller>(tag: 'two'), true);
Get.reset();
});
... ... @@ -68,12 +65,9 @@ void main() {
Get.lazyPut<Controller>(() => Controller(), tag: 'one');
Get.lazyPut<Controller>(() => Controller(), tag: 'two');
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'two'),
false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'one'),
true);
expect(Get.find<Controller>(tag: 'two') == Get.find<Controller>(tag: 'two'),
true);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'two'), false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'one'), true);
expect(Get.find<Controller>(tag: 'two') == Get.find<Controller>(tag: 'two'), true);
Get.reset();
});
... ... @@ -101,8 +95,7 @@ void main() {
expect(Get.find<Controller>().count, 1);
Get.delete<Controller>();
expect(
() => Get.find<Controller>(), throwsA(const m.TypeMatcher<String>()));
expect(() => Get.find<Controller>(), throwsA(const m.TypeMatcher<String>()));
Get.reset();
});
... ... @@ -162,12 +155,10 @@ void main() {
test('Get.delete test with disposable controller', () async {
// Get.put(DisposableController());
expect(Get.delete<DisposableController>(), true);
expect(() => Get.find<DisposableController>(),
throwsA(const m.TypeMatcher<String>()));
expect(() => Get.find<DisposableController>(), throwsA(const m.TypeMatcher<String>()));
});
test('Get.put test after delete with disposable controller and init check',
() async {
test('Get.put test after delete with disposable controller and init check', () async {
final instance = Get.put<DisposableController>(DisposableController());
expect(instance, Get.find<DisposableController>());
expect(instance.initialized, true);
... ... @@ -253,6 +244,20 @@ void main() {
expect((Get.find<DisposableController>() as Controller).count, 0);
});
});
group('Get.findOrNull test', () {
tearDown(Get.reset);
test('checking results', () async {
Get.put<int>(1);
int? result = Get.findOrNull<int>();
expect(result, 1);
Get.delete<int>();
result = Get.findOrNull<int>();
expect(result, null);
});
});
}
class PermanentService extends GetxService {}
... ...