Skip to content

Remove allowMultipleInstances #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>([String instanceName]) {
throwIfNot(
Expand Down
25 changes: 0 additions & 25 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestBaseClass>(() => TestClass());
secondGetIt.registerLazySingleton<TestBaseClass>(() => TestClass());

var instance1 = GetIt.I<TestBaseClass>();

expect(instance1 is TestClass, true);

var instance2 = GetIt.I.get<TestBaseClass>();

expect(instance1, instance2);
expect(constructorCounter, 1);

var instanceSecondGetIt = secondGetIt.get<TestBaseClass>();

expect(instance1, isNot(instanceSecondGetIt));
expect(constructorCounter, 2);

GetIt.I.reset();
});

test('trying to access not registered type', () {
var getIt = GetIt.instance;

Expand Down