provideSingleton<T> method

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

Registers a singleton factory for type T.

When lazy is true (the default), factory is evaluated on the first inject call and the resulting instance is cached for all future resolutions. When lazy is false, factory is executed immediately upon registration.

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

Implementation

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