get<T extends Object> method

  1. @override
T get<T extends Object>()
override

Implementation

@override
T get<T extends Object>() {
  if (_singletons[T] case final T value?) {
    return value;
  }

  if (_lazySingletons[T] case final T Function() factory?) {
    return _singletons[T] = factory();
  }

  if (_factories[T] case final T Function() factory?) {
    return factory();
  }

  // Reached only outside a request — the per-request container resolves
  // these itself. Building one here would hand back an instance nothing
  // ever disposes and that no other caller in the request would share,
  // which is exactly the bug request scoping exists to prevent.
  if (_requestScoped.containsKey(T)) {
    throw StateError(
      '$T is registered as request scoped and cannot be resolved outside '
      'a request. It is available in handlers, middleware, guards, '
      'interceptors and exception catchers.',
    );
  }

  throw Exception(
    'Nothing found for type $T within $DI, did you forget to register it?',
  );
}