lazyPut<S> method

void lazyPut<S>(
  1. S builder(), {
  2. String? tag,
  3. bool permanent = false,
  4. bool isFactory = false,
})

Registers a lazy builder in this scope.

The builder is executed only when the dependency is first requested via find.

Parameters:

  • builder: A function that creates the instance.
  • tag: Optional unique identifier for the instance.
  • permanent: If true, the instance survives a non-forced reset.
  • isFactory: If true, a new instance is created for every find call.

Implementation

void lazyPut<S>(S Function() builder,
    {String? tag, bool permanent = false, bool isFactory = false}) {
  final key = _getKey<S>(tag);

  if (!isFactory &&
      _registry.containsKey(key) &&
      _registry[key]!.isInstantiated) {
    return;
  }

  final info = LevitDependency<S>(
    builder: builder,
    permanent: permanent || isFactory,
    isLazy: true,
    isFactory: isFactory,
  );

  _registerBinding(key, info, isFactory ? 'putFactory' : 'lazyPut', tag: tag);
}