elementAt method

  1. @override
EasyText elementAt(
  1. int index
)
override

Returns the EasyText element at the specified index with optimization for consecutive access to the same index.

This implementation includes a caching mechanism that remembers the last accessed element and index. If the same index is requested again and the cached element is still valid and in this list, it returns the cached element directly, avoiding list iteration.

Performance Note:

For large lists, this optimization can significantly improve access times when accessing the same position repeatedly.

Throws a RangeError if index is out of bounds.

Implementation

@override
EasyText elementAt(int index) {
  // Make a direct jump to the text to avoid iterating
  if (index == lastIndex) {
    if (lastUsed != null &&
        lastUsed!.list != null &&
        lastUsed!.list == this) {
      return lastUsed!;
    }
  }
  final EasyText text = super.elementAt(index);
  lastUsed = text;
  lastIndex = index;
  return text;
}