getIndex<T> method

T getIndex<T>(
  1. int index
)

Gets the value at the specified index in the CborListValue.

If index is out of bounds and T is nullable, returns null. Otherwise, throws a MessageException.

Implementation

T getIndex<T>(int index) {
  if (index >= value.length) {
    if (null is T) return null as T;
    throw MessageException("Index out of bounds.",
        details: {"length": value.length, "index": index});
  }

  final CborObject obj = value.elementAt(index);
  if (null is T && obj == const CborNullValue()) {
    return null as T;
  }
  if (obj is T) return obj as T;
  if (obj.value is! T) {
    throw MessageException("Failed to cast value.",
        details: {"Excepted": obj.value.runtimeType, "Type": "$T"});
  }
  return obj.value;
}