Skip to content

Commit 8d745af

Browse files
authored
Merge pull request #355 from tpucci/tpucci-getall
fix: fix and test getAll
2 parents 6f539f4 + 8efccb6 commit 8d745af

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/get_it_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ class _GetItImplementation implements GetIt {
10571057
}
10581058

10591059
final typeRegistration = registrationScope.typeRegistrations
1060-
.putIfAbsent(T, () => _TypeRegistration());
1060+
.putIfAbsent(T, () => _TypeRegistration<T>());
10611061

10621062
final serviceFactory = _ServiceFactory<T, P1, P2>(
10631063
this,

test/get_it_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,31 @@ void main() {
402402
GetIt.I.reset();
403403
});
404404

405+
test('get all registered instances of the same type', () {
406+
final getIt = GetIt.instance;
407+
GetIt.allowRegisterMultipleImplementationsOfoneType = true;
408+
constructorCounter = 0;
409+
410+
getIt.registerLazySingleton<TestBaseClass>(
411+
() => TestClass(),
412+
);
413+
getIt.registerLazySingleton<TestBaseClass>(
414+
() => TestClass(),
415+
);
416+
417+
expect(constructorCounter, 0);
418+
419+
final Iterable<TestBaseClass> instances = getIt.getAll<TestBaseClass>();
420+
421+
expect(instances.length, 2);
422+
expect(instances.first is TestClass, true);
423+
expect(instances.last is TestClass, true);
424+
expect(constructorCounter, 2);
425+
426+
GetIt.I.reset();
427+
GetIt.allowRegisterMultipleImplementationsOfoneType = false;
428+
});
429+
405430
test('reset lazy Singleton when the disposing function is a future',
406431
() async {
407432
final getIt = GetIt.instance;

0 commit comments

Comments
 (0)