leafMostValueMatching method

T? leafMostValueMatching(
  1. Path relativePath,
  2. bool predicate(
    1. T value
    )
)

The leaf-most value on relativePath matching predicate, or null.

Implementation

T? leafMostValueMatching(
  Path relativePath,
  bool Function(T value) predicate,
) {
  final T? value = this.value;
  T? currentValue = (value != null && predicate(value)) ? value : null;
  ImmutableTree<T> current = this;
  for (final ChildKey key in relativePath.segments) {
    final ImmutableTree<T>? next = current.children[key];
    if (next == null) {
      return currentValue;
    }
    final T? nextValue = next.value;
    if (nextValue != null && predicate(nextValue)) {
      currentValue = nextValue;
    }
    current = next;
  }
  return currentValue;
}