findInThisScope<T> method

T? findInThisScope<T>({
  1. String? tag,
})

Find a dependency only in this specific scope (not in parents)

Implementation

T? findInThisScope<T>({String? tag}) {
  if (_disposed) {
    return null;
  }

  final key = _makeKey<T>(tag);

  if (tag != null) {
    // Exact (T, tag) lookup first
    final exact = _taggedBindings[T]?[tag];
    if (exact != null) {
      return exact as T;
    }

    // Fallback: allow subtype lookup (e.g., put<Impl>(x, tag: 'a'), find<Base>(tag: 'a'))
    for (final tagMap in _taggedBindings.values) {
      final candidate = tagMap[tag];
      if (candidate != null && candidate is T) {
        return candidate;
      }
    }

    // Check for factory
    if (_factories.containsKey(key)) {
      return _createFromFactory<T>(key, tag);
    }

    return null;
  } else {
    // Try to find by type in this scope only
    final instance = _typeBindings[T];
    if (instance != null) {
      return instance as T;
    }

    // Check for factory
    if (_factories.containsKey(key)) {
      return _createFromFactory<T>(key, null);
    }

    return null;
  }
}