readOrNull<T> method
Reads the nearest instance of T provided by this node or an
ancestor, checking this node first. Returns null if none was provided.
Not reactive: whether it finds a match or not, the result is cached
until the node is unmounted, so a later provide call for T won't be
picked up until then.
Throws a StateError if this node is not mounted yet.
Implementation
T? readOrNull<T>() {
final dependencies = _dependencies ??= {};
if (dependencies.containsKey(T)) return dependencies[T] as T?;
if (!isMounted) {
throw StateError('Cannot read $T because this node is not mounted yet.');
}
Node? node = this;
while (node != null) {
final providers = node._providers;
if (providers != null && providers.containsKey(T)) {
return dependencies[T] = providers[T] as T;
}
node = node.parent;
}
return dependencies[T] = null;
}