registerSingleton<T> method

T registerSingleton<T>(
  1. T object, {
  2. Type? as,
})

Registers a singleton. Any attempt to resolve the type within this container will return object.

Returns object.

Implementation

T registerSingleton<T>(T object, {Type? as}) {
  as ??= T == dynamic ? as : T;

  if (_singletons.containsKey(as ?? object.runtimeType)) {
    throw StateError('This container already has a singleton for ${as ?? object.runtimeType}.');
  }

  _singletons[as ?? object.runtimeType] = object;
  return object;
}