decode method

  1. @override
(dynamic, int) decode(
  1. Uint8List data,
  2. int offset
)
override

Decodes a value of this type from data at the given offset. Returns the decoded value and the number of bytes consumed.

Implementation

@override
(dynamic, int) decode(Uint8List data, int offset) {
  var currentOffset = offset;
  int arrayLength;

  if (length == null) {
    final (len, _) = AbiUint(256).decode(data, currentOffset);
    arrayLength = (len as BigInt).toInt();
    currentOffset += 32;
  } else {
    arrayLength = length!;
  }

  final result = <dynamic>[];

  if (elementType.isDynamic) {
    // Read offsets first
    final offsets = <int>[];
    for (var i = 0; i < arrayLength; i++) {
      final (off, _) = AbiUint(256).decode(data, currentOffset + i * 32);
      offsets.add((off as BigInt).toInt());
    }

    // Decode elements at offsets
    for (final off in offsets) {
      final (element, _) = elementType.decode(data, currentOffset + off);
      result.add(element);
    }
  } else {
    for (var i = 0; i < arrayLength; i++) {
      final (element, consumed) = elementType.decode(data, currentOffset);
      result.add(element);
      currentOffset += consumed;
    }
  }

  return (result, currentOffset - offset);
}