get<T> static method

ValueNotifier<T>? get<T>(
  1. String key
)

Allows to get an inherited "value" and add listener for its changes. Used in child components.

Also removes the listener when the component is disposed.

Implementation

static ValueNotifier<T>? get<T>(String key) {
  return VComponent.run((vComponent) {
    try {
      final context = VContext(context: vComponent.context, key: _key);
      final Map<String, ValueNotifier> notifiers =
          context.init('notifiers', () => {});
      if (!notifiers.containsKey(key)) {
        VNode? parent = vComponent;
        while (parent != null) {
          if (parent.kind == VNodeKind.component) {
            parent = parent as VComponent;
            final parentContext =
                VContext(context: parent.context, key: _key);
            if (parentContext.hasKey('notifiers')) {
              final Map<String, ValueNotifier> parentNotifiers =
                  parentContext.get('notifiers');
              if (parentNotifiers.containsKey(key)) {
                final notifier = parentNotifiers[key] as ValueNotifier<T>;
                // ignore: prefer_function_declarations_over_variables
                final h = () {
                  VRenderer.render(vComponent);
                };

                notifiers[key] = notifier;
                notifier.addListener(h);
                vComponent.addDisposeHandler(vComponent, () {
                  notifier.removeListener(h);
                });

                return notifier;
              }
            }
          }

          parent = parent.parent;
        }

        return null;
      } else {
        final notifier = notifiers[key];
        return notifier as ValueNotifier<T>;
      }
    } catch (e, s) {
      throw WrappedException(
          "An error occurred while executing the method '$InheritedValue.get($key)' for component '$vComponent'",
          e,
          s);
    }
  });
}