putLazy<T> static method
Register a lazy factory in root scope
Creates a singleton that is instantiated only on first access.
- Set
isPermanentto true for permanent singletons (survive scope cleanup) - Set
isPermanentto false for temporary singletons (default, cleaned up with scope) - Set
alwaysNewto true to create fresh instance on each find() call (factory pattern)
Note: Cannot set both isPermanent and alwaysNew to true
Example:
// Lazy singleton (created once, temporary)
Zen.putLazy(() => HeavyService());
// Permanent lazy singleton
Zen.putLazy(() => ConfigService(), isPermanent: true);
// Factory pattern (new instance each time)
Zen.putLazy(() => RequestId.generate(), alwaysNew: true);
Implementation
static void putLazy<T>(
T Function() factory, {
String? tag,
bool isPermanent = false,
bool alwaysNew = false,
}) {
rootScope.putLazy<T>(
factory,
tag: tag,
isPermanent: isPermanent,
alwaysNew: alwaysNew,
);
}