layerByName method

Layer layerByName(
  1. String name
)

Finds the first layer with the matching name, or throw an ArgumentError if one cannot be found. Will search recursively through Group children.

Implementation

Layer layerByName(String name) {
  final toSearch = Queue<List<Layer>>();
  toSearch.add(layers);

  Layer? found;
  while (found == null && toSearch.isNotEmpty) {
    final currentLayers = toSearch.removeFirst();
    currentLayers.forEach((layer) {
      if (layer.name == name) {
        found = layer;
        return;
      } else if (layer is Group) {
        toSearch.add(layer.layers);
      }
    });
  }

  if (found != null) {
    return found!;
  }

  // Couldn't find it in any layer
  throw ArgumentError('Layer $name not found');
}