registerSingle<T, R extends T> method

void registerSingle<T, R extends T>(
  1. SpotGetter<R> locator, {
  2. String? name,
})

Register a singleton that returns the same instance on each resolution.

The singleton is scoped to this container. If a parent has a singleton with the same key, this registration shadows it for this scope and children.

Type Parameters:

  • T: The interface or base type to register
  • R: The concrete implementation (must extend or implement T)

Parameters:

  • locator: Factory function called once to create the singleton
  • name: Optional name qualifier for named instances

Implementation

void registerSingle<T, R extends T>(SpotGetter<R> locator, {String? name}) {
  final key = SpotKey<T>(T, name);

  if (registry.containsKey(key) && logging) {
    log.w('Overriding singleton in scope: $key with $R');
  }

  registry[key] = SpotService<T>(SpotType.singleton, locator as SpotGetter<T>, R);

  if (logging) log.v('Registered singleton in scope: $key -> $R');
}