provideSingleton<T> method

void provideSingleton<T>(
  1. FactoryFunc<T> factory, {
  2. bool lazy = true,
})

Registers a singleton factory for type T.

The factory creates a single instance that is cached and returned for all future resolutions of T.

If lazy is true (the default), the instance is created on first inject. If lazy is false, the instance is instantiated immediately upon registration.

Example

container.provideSingleton<AuthService>(() => AuthService(), lazy: true);

Implementation

void provideSingleton<T>(FactoryFunc<T> factory, {bool lazy = true}) {
  _bindings[T] = _Binding<T>.singleton(factory, lazy: lazy);
}