findOrNull<S> method

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

Resolves the registered instance of type S, or null if not found.

Parameters:

  • tag: Optional unique identifier for the instance.

Implementation

S? findOrNull<S>({String? tag}) {
  final key = _getKey<S>(tag);

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

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

  // 3. Try Parent
  if (_parentScope != null) {
    final instance = _parentScope!.findOrNull<S>(tag: tag);
    if (instance != null) {
      _cacheScope(key, _parentScope!);
      return instance;
    }
  }

  return null;
}