Skip to content
Open
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
3 changes: 3 additions & 0 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,14 @@ abstract class GetIt {
/// if referenceCount is 0
/// [ignoreReferenceCount] if `true` it will ignore the reference count and unregister the object
/// only use this if you know what you are doing
/// [ignoreIfNotRegistered] if `true` a missing registration is a no-op instead of
/// throwing. Defaults to `false` so a second dispose still surfaces a logic error.
FutureOr unregister<T extends Object>({
Object? instance,
String? instanceName,
FutureOr Function(T)? disposingFunction,
bool ignoreReferenceCount = false,
bool ignoreIfNotRegistered = false,
});

/// returns a Future that completes if all asynchronously created Singletons and any
Expand Down
20 changes: 17 additions & 3 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1414,17 +1414,31 @@ class _GetItImplementation implements GetIt {
/// If you have provided an disposing function when you registered the object that one will be called automatically
/// If you have enabled reference counting when registering, [unregister] will only unregister and dispose the object
/// if referenceCount is 0
/// [ignoreIfNotRegistered] if `true` a missing registration is a no-op instead of
/// throwing. Defaults to `false`.
///
@override
FutureOr unregister<T extends Object>({
Object? instance,
String? instanceName,
FutureOr Function(T)? disposingFunction,
bool ignoreReferenceCount = false,
bool ignoreIfNotRegistered = false,
}) async {
final registrationToRemove = instance != null
? _findRegistrationByInstance(instance)
: _findRegistrationByNameAndType<T>(instanceName);
final _ObjectRegistration? registrationToRemove;
if (instance != null) {
registrationToRemove = ignoreIfNotRegistered
? _findFirstRegistrationByInstanceOrNull(instance)
: _findRegistrationByInstance(instance);
} else {
registrationToRemove = ignoreIfNotRegistered
? _findFirstRegistrationByNameAndTypeOrNull<T>(instanceName)
: _findRegistrationByNameAndType<T>(instanceName);
}

if (registrationToRemove == null) {
return;
}

throwIf(
registrationToRemove.objectsWaiting.isNotEmpty,
Expand Down
54 changes: 54 additions & 0 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,60 @@ void main() {
throwsStateError,
);
});

test('unregister throws by default if not registered', () async {
final getIt = GetIt.instance;

await expectLater(getIt.unregister<TestClass>(), throwsStateError);

getIt.registerSingleton<TestClass>(TestClass());
await getIt.unregister<TestClass>();

await expectLater(getIt.unregister<TestClass>(), throwsStateError);
});

test('unregister ignoreIfNotRegistered no-ops if missing', () async {
final getIt = GetIt.instance;
disposeCounter = 0;

await getIt.unregister<TestClass>(ignoreIfNotRegistered: true);

getIt.registerSingleton<TestClass>(
TestClass(),
dispose: (testClass) {
testClass.dispose();
},
);

await getIt.unregister<TestClass>();
expect(disposeCounter, 1);
expect(getIt.isRegistered<TestClass>(), isFalse);

await getIt.unregister<TestClass>(ignoreIfNotRegistered: true);
expect(disposeCounter, 1);
expect(getIt.isRegistered<TestClass>(), isFalse);
});

test('unregister ignoreIfNotRegistered by instance no-ops if missing',
() async {
final getIt = GetIt.instance;
final instance = TestClass();

await getIt.unregister(
instance: instance,
ignoreIfNotRegistered: true,
);

getIt.registerSingleton<TestClass>(instance);
await getIt.unregister(instance: instance);

await getIt.unregister(
instance: instance,
ignoreIfNotRegistered: true,
);
expect(getIt.isRegistered<TestClass>(), isFalse);
});

test('change registration name with type and name', () async {
final getIt = GetIt.instance;
disposeCounter = 0;
Expand Down