lazyPutAsync<S> method

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

Registers an asynchronous lazy builder in this scope.

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

Parameters:

  • builder: An async 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 future is created for every findAsync call.

Implementation

Future<S> Function() lazyPutAsync<S>(
  Future<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 () => findAsync<S>(tag: tag);
  }

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

  _registerBinding(key, info, isFactory ? 'putFactoryAsync' : 'lazyPutAsync',
      tag: tag);

  return () => findAsync<S>(tag: tag);
}