From c234e7cb706e10995d100877146c0fe6b63e8ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Betts?= Date: Fri, 8 Nov 2019 13:49:29 -0800 Subject: [PATCH 1/2] Remove allowMultipleInstances When the user calls `asNewInstance`, they are already indicating that they know what they're doing - we don't need to add an extra safety to this --- lib/get_it.dart | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/lib/get_it.dart b/lib/get_it.dart index 371d2fe..bd29f5b 100644 --- a/lib/get_it.dart +++ b/lib/get_it.dart @@ -41,26 +41,14 @@ class GetIt { static GetIt get I => instance; /// You should prefer to use the `instance()` method to access an instance of [GetIt]. - /// If you really, REALLY need more than one [GetIt] instance please set allowMultipleInstances - /// to true to signal you know what you are doing :-). factory GetIt.asNewInstance() { - throwIfNot( - allowMultipleInstances, - StateError( - 'You should prefer to use the `instance()` method to access an instance of GetIt. ' - 'If you really need more than one GetIt instance please set allowMultipleInstances to true.'), - ); return GetIt._(); } /// By default it's not allowed to register a type a second time. /// If you really need to you can disable the asserts by setting[allowReassignment]= true bool allowReassignment = false; - - /// By default it's not allowed to create more than one [GetIt] instance. - /// If you really need to you can disable the asserts by setting[allowReassignment]= true - static bool allowMultipleInstances = false; - + /// retrieves or creates an instance of a registered type [T] depending on the registration function used for this type or based on a name. T get([String instanceName]) { throwIfNot( From a5ed4d8f91421e44f26f42d1d1e5dd56616f5cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Betts?= Date: Fri, 8 Nov 2019 13:52:02 -0800 Subject: [PATCH 2/2] Fixup the tests --- test/get_it_test.dart | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/test/get_it_test.dart b/test/get_it_test.dart index 4391f87..c267246 100644 --- a/test/get_it_test.dart +++ b/test/get_it_test.dart @@ -105,31 +105,6 @@ void main() { GetIt.I.reset(); }); - test('register lazy singleton two instances of GetIt', () { - GetIt.allowMultipleInstances = true; - var secondGetIt = GetIt.asNewInstance(); - - constructorCounter = 0; - GetIt.instance.registerLazySingleton(() => TestClass()); - secondGetIt.registerLazySingleton(() => TestClass()); - - var instance1 = GetIt.I(); - - expect(instance1 is TestClass, true); - - var instance2 = GetIt.I.get(); - - expect(instance1, instance2); - expect(constructorCounter, 1); - - var instanceSecondGetIt = secondGetIt.get(); - - expect(instance1, isNot(instanceSecondGetIt)); - expect(constructorCounter, 2); - - GetIt.I.reset(); - }); - test('trying to access not registered type', () { var getIt = GetIt.instance;