putLazy<T> static method

void putLazy<T>(
  1. T factory(), {
  2. String? tag,
  3. bool isPermanent = false,
  4. bool alwaysNew = false,
})

Register a lazy factory in root scope

Creates a singleton that is instantiated only on first access.

  • Set isPermanent to true for permanent singletons (survive scope cleanup)
  • Set isPermanent to false for temporary singletons (default, cleaned up with scope)
  • Set alwaysNew to 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,
  );
}