getNodeAt<NodeType extends DocumentNode> static method

NodeType getNodeAt<NodeType extends DocumentNode>(
  1. int index, [
  2. Finder? superDocumentFinder
])

Returns the DocumentNode at given the index.

The given index must be a valid node index inside the Document.The node at index must be of type NodeType.

By default, this method expects a single SuperReader in the widget tree and finds it byType. To specify one SuperReader among many, pass a superDocumentFinder.

Implementation

static NodeType getNodeAt<NodeType extends DocumentNode>(int index, [Finder? superDocumentFinder]) {
  final doc = findDocument(superDocumentFinder);

  if (doc == null) {
    throw Exception('SuperReader not found');
  }

  if (index >= doc.nodes.length) {
    throw Exception('Tried to access index $index in a document where the max index is ${doc.nodes.length - 1}');
  }

  final node = doc.nodes[index];
  if (node is! NodeType) {
    throw Exception('Tried to access a ${node.runtimeType} as $NodeType');
  }

  return node;
}