findOrNullAsync<S> method

Future<S?> findOrNullAsync<S>({
  1. dynamic key,
  2. String? tag,
})

Asynchronously resolves a dependency, or null if not found.

Implementation

Future<S?> findOrNullAsync<S>({dynamic key, String? tag}) async {
  try {
    if (key is LevitStore) {
      final scope = _ScopeProvider.of(_context) ?? Ls.currentScope;
      final result = await key.findAsyncIn(scope, tag: tag);
      if (result is Future) return await result as S?;
      return result as S?;
    }
    if (key is LevitAsyncStore) {
      final scope = _ScopeProvider.of(_context) ?? Ls.currentScope;
      return await key.findAsyncIn(scope, tag: tag) as S?;
    }
    final scope = _ScopeProvider.of(_context);
    if (scope != null) {
      return await scope.findOrNullAsync<S>(tag: tag);
    }
    return await Levit.findOrNullAsync<S>(tag: tag);
  } catch (_) {
    return null;
  }
}