lazyPut<S> method
Registers a dependency to be lazily instantiated.
The builder is only executed when the dependency is first requested via find.
If isFactory is true, a new instance will be created for every request.
Otherwise (default), the instance is cached and treated as a singleton within this scope.
Use permanent to survive a non-forced reset only.
dispose always resets with force: true. Prefer permanent on root /
long-lived scopes.
Implementation
void lazyPut<S>(S Function() builder,
{String? tag, bool permanent = false, bool isFactory = false}) {
_ensureUsable();
final key = _getKey<S>(tag);
final keyString = key.debugString;
if (_aliases.containsKey(key)) {
throw StateError(
'LevitScope($name): "$keyString" is already registered as an alias.',
);
}
if (!isFactory &&
_registry.containsKey(key) &&
_registry[key]!.isInstantiated) {
return;
}
final info = LevitDependency<S>(
builder: builder,
permanent: permanent || isFactory,
isLazy: true,
isFactory: isFactory,
);
_registerBinding(
key,
keyString,
info,
isFactory ? 'putFactory' : 'lazyPut',
tag: tag,
);
}