getHierarchyDownTo method

List<Module>? getHierarchyDownTo(
  1. Module instance
)

Returns a list of instances between calling module this and the provided instance, where the first element is this and the last element is instance.

If there is no path found, returns null. If instance is the same as this, returns null.

Implementation

List<Module>? getHierarchyDownTo(Module instance) {
  if (instance == this) {
    return null;
  }

  Module? parent = instance;

  final fullPath = <Module>[];

  while (parent != null) {
    fullPath.add(parent);

    if (parent == this) {
      break;
    }

    parent = parent.parent;
  }

  if (parent != this) {
    return null;
  }

  return fullPath.reversed.toList(growable: false);
}