findOrNull<S> method

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

Resolves a dependency, or null if not found.

Implementation

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