findAsync<S> method

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

Asynchronously resolves the registered instance of type S.

Parameters:

  • tag: Optional unique identifier for the instance.

Throws an Exception if no registration is found.

Implementation

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

  // 1. Try Local
  final info = _registry[key];
  if (info != null) {
    return _findLocalAsync<S>(info as LevitDependency<S>, key, tag);
  }

  // 2. Try Cache
  final cachedScope = _resolutionCache[key];
  if (cachedScope != null) {
    return cachedScope.findAsync<S>(tag: tag);
  }

  // 3. Try Parent
  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.',
  );
}