registerSingletonAsync<T extends Object> method
registers a type as Singleton by passing an asynchronous factory function which has to return the instance
that will be returned on each call of get on that type.
Therefore you have to ensure that the instance is ready before you use get on it or use getAsync() to
wait for the completion.
You can wait/check if the instance is ready by using isReady() and isReadySync().
factoryfunc
is executed immediately if there are no dependencies to other Singletons (see below).
As soon as it returns, this instance is marked as ready unless you don't set signalsReady==true
instanceName
if you provide a value here your instance gets registered with that
name instead of a type. This should only be necessary if you need to register more
than one instance of one type. Its highly not recommended
dependsOn
if this instance depends on other registered Singletons before it can be initilaized
you can either orchestrate this manually using isReady() or pass a list of the types that the
instance depends on here. factoryFunc
won't get executed till this types are ready.
If signalsReady
is set to true
it means that the future you can get from allReady()
cannot complete until this
this instance was signalled ready by calling signalsReady(instance)
. In that case no automatic ready signal
is made after completion of factoryfunc
Implementation
void registerSingletonAsync<T extends Object>(
FactoryFuncAsync<T> factoryfunc, {
String? instanceName,
Iterable<Type>? dependsOn,
bool? signalsReady,
DisposingFunc<T>? dispose,
Set<String>? registerFor,
}) {
if (_canRegister(registerFor)) {
locator.registerSingletonAsync<T>(
factoryfunc,
instanceName: instanceName,
dependsOn: dependsOn,
signalsReady: signalsReady,
dispose: dispose,
);
}
}