elementAt method

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

Returns the element of type T at the specified index.

Implementation

@override
T elementAt(int index) {
  // TODO: this is identical to list[] - consider refactoring to combine them.
  if (index < 0 || index >= length) {
    throw RangeError.range(index, 0, length - 1);
  }

  var value = handle.elementAt(realm, _skipOffset + index);

  if (value is ObjectHandle) {
    late RealmObjectMetadata targetMetadata;
    late Type type;
    if (T == RealmValue) {
      (type, targetMetadata) = realm.metadata.getByClassKey(value.classKey);
    } else {
      targetMetadata = _metadata!;
      type = T;
    }
    value = realm.createObject(type, value, targetMetadata);
  }

  if (T == RealmValue) {
    value = RealmValue.from(value);
  }

  return value as T;
}