findAsync<S> method

Future<S> findAsync<S>({
  1. String? tag,
})

Finds and returns an asynchronously registered instance of type S.

Use this for dependencies registered via lazyPutAsync.

Throws an Exception if S is not registered.

Implementation

Future<S> findAsync<S>({String? tag}) async {
  _ensureUsable();
  final key = _getKey<S>(tag);
  final keyString = key.debugString;

  // Async lookup follows the same local/cache/parent search order.
  final info = _registry[key];
  if (info != null) {
    return _findLocalAsync<S>(
        info as LevitDependency<S>, key, keyString, tag);
  }

  final canonicalKey = _aliases[key];
  if (canonicalKey != null) {
    return _findAliasLocalAsync<S>(
      aliasKey: key,
      canonicalKey: canonicalKey,
    );
  }

  final cachedScope = _readCachedScope(key);
  if (cachedScope != null) {
    try {
      return await cachedScope.findAsync<S>(tag: tag);
    } catch (_) {
      _resolutionCache.remove(key);
    }
  }

  if (_parentScope != null) {
    try {
      final instance = await _parentScope!.findAsync<S>(tag: tag);
      _cacheScope(key, _parentScope!);
      return instance;
    } catch (_) {}
  }

  throw Exception(
    'LevitScope($name): Type "$S"${tag != null ? ' with tag "$tag"' : ''} is not registered.\n'
    'Not found in scope or any parent.',
  );
}