registerSingletonIfAbsent<T extends Object> abstract method

T registerSingletonIfAbsent<T extends Object>(
  1. T factoryFunc(), {
  2. String? instanceName,
  3. DisposingFunc<T>? dispose,
})

---- With reference counting ----

registerSingletonIfAbsent and releaseInstance are used to manage the lifecycle of a Singleton. This is useful if you register an object when you push a Page and this page can get pushed recursively. In that case you don't want to dispose the object when first of these pages is popped

Only registers a type new as Singleton if it is not already registered. Otherwise it returns the existing instance and increments an internal reference counter to ensure that matching unregister or releaseInstance calls will decrement the reference counter an won't unregister and dispose the registration as long as the reference counter is > 0. T type/interface that is used for the registration and the access via get factoryFunc that is called to create the instance if it is not already registered instanceName optional key to register more than one instance of one type dispose disposing function that is automatically called before the object is removed from get_it

Implementation

/// Only registers a type new as Singleton if it is not already registered. Otherwise it returns
/// the existing instance and increments an internal reference counter to ensure that matching
/// [unregister] or [releaseInstance] calls will decrement the reference counter an won't unregister
/// and dispose the registration as long as the reference counter is > 0.
/// [T] type/interface that is used for the registration and the access via [get]
/// [factoryFunc] that is called to create the instance if it is not already registered
/// [instanceName] optional key to register more than one instance of one type
/// [dispose] disposing function that is automatically called before the object is removed from get_it
T registerSingletonIfAbsent<T extends Object>(
  T Function() factoryFunc, {
  String? instanceName,
  DisposingFunc<T>? dispose,
});