resolve<T> function

T resolve<T>(
  1. {String name}
)

This function resolves a service determined by the type T and an optional name.

Throws a InjectionContextNotInitialized exception, if the injection context is not started.

Throws a InjectionContextHasNoService exception, if the service that shall be resolved was not registered before.

Implementation

T resolve<T>({String name}) {
  var services = _ContextCollection.shared._activeContexts
      .where((context) => context._hasService<T>(name: name))
      .map<T>((context) => context.resolve<T>(name: name))
      .toList();

  if (services.length > 1) {
    throw InjectionContextHasMoreThanOneService(services);
  } else if (services.isEmpty) {
    throw InjectionContextHasNoService(_stringOfType<T>(), (name ?? _stringOfType<T>()));
  }

  return services.first;
}