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}) {
  Type t2 = T;
  if (as != null) {
    t2 = as;
  } else if (T == dynamic) {
    t2 = as ?? object.runtimeType;
  }
  //as ??= T == dynamic ? as : T;

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

  _singletons[t2] = object;
  return object;
}