get<T> method

T get<T>({
  1. String dependencyName = "",
})

Returns the registered dependencies with the signature of T and the optional dependencyName.

Throws NotDefinedException when the requested dependency has not been registered yet.

Throws CircularDependencyException when the injector detected a circular dependency setup.

Implementation

T get<T>({String dependencyName = ""}) {
  _checkValidation<T>();

  final identity = _getIdentity<T>(dependencyName);

  final factory = _factoryMap[identity];

  if (factory == null) {
    throw NotDefinedException(type: T.toString());
  }

  final factoryId = factory.hashCode;

  final unique = _factoryCallIds.add(factoryId);
  if (!unique) {
    throw CircularDependencyException(type: T.toString());
  }

  try {
    final instance = factory.instance as T;
    _factoryCallIds.remove(factoryId);
    return instance;
    // ignore: avoid_catches_without_on_clauses
  } catch (e) {
    // In case something went wrong, we have to clear the called factory list
    // because this will trigger a [CircularDependencyException] the next time
    // this factory is called again.
    _factoryCallIds.clear();
    rethrow;
  }
}