getValue<T> method

T? getValue<T>(
  1. ContextKey key
)

Returns the value identified by key, or null if no such value exists.

Implementation

T? getValue<T>(ContextKey key) {
  // check this context version or its previous versions
  final value = _key == key ? _value : _parent?._getValue(key);
  if (value != null) {
    return value as T;
  }

  // check other contexts within the current zone or a parent zone
  Zone? zone = Zone.current;
  while (zone != null) {
    final stack = _stacks[zone];
    if (stack != null && stack.isNotEmpty) {
      for (final entry in stack.reversed) {
        final value = entry.context._getValue(key);
        if (value != null) {
          return value as T;
        }
      }
    }
    zone = zone.parent;
  }
  return null;
}