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 a list with just the one module in it.

Implementation

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

  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);
}