get<T> static method

T get<T>({
  1. String? name,
})

Gets an instance of the given type. Throws StateError if the type is not registered.

Implementation

static T get<T>({String? name}) {
  if (name != null) {
    final key = '${T.toString()}_$name';
    final instance = _namedInstances[key];
    if (instance == null) {
      throw StateError(
          'No instance registered for type $T with name "$name"');
    }
    return instance as T;
  }

  // Try to get from instances first
  final instance = _instances[T];
  if (instance != null) {
    return instance as T;
  }

  // Try to create from provider
  final provider = _providers[T] as BaseProvider<T>?;
  if (provider != null) {
    final created = provider.create();
    _instances[T] = created; // Cache the instance
    return created;
  }

  throw StateError('No instance or provider registered for type $T');
}