unregister<T> method

void unregister<T>(
  1. {Object instance,
  2. String instanceName,
  3. void disposingFunction(
    1. T
    )}
)

Unregister an instance of an object or a factory/singleton by Type T or by name instanceName if you need to dispose any resources you can do it using disposingFunction function that provides a instance of your class to be disposed

Implementation

void unregister<T>(
    {Object instance,
    String instanceName,
    void Function(T) disposingFunction}) {
  if (instance != null) {
    var registeredInstance = _factories.values
        .followedBy(_factoriesByName.values)
        .where((x) => identical(x.instance, instance));

    throwIf(
      registeredInstance.isEmpty,
      ArgumentError.value(instance,
          'There is no object type ${instance.runtimeType} registered in GetIt'),
    );

    var _factory = registeredInstance.first;
    if (_factory.isNamedRegistration) {
      _factoriesByName.remove(_factory.instanceName);
    } else {
      _factories.remove(_factory.registrationType);
    }
    disposingFunction?.call(_factory.instance);
  } else {
    throwIf(
      (((const Object() is! T) && instanceName != null)),
      ArgumentError(
          'GetIt: You have to provide either a type OR a name not both.'),
    );
    throwIfNot(
      (instanceName != null && _factoriesByName.containsKey(instanceName)) ||
          _factories.containsKey(T),
      ArgumentError(
          'No Type registered ${T.toString()} or instance Name must not be null'),
    );
    if (instanceName == null) {
      disposingFunction?.call(get<T>());
      _factories.remove(T);
    } else {
      disposingFunction?.call(get(instanceName));
      _factoriesByName.remove(instanceName);
    }
  }
}