fixedArray<T, Input> static method

BcsType<List<T>, Iterable<Input>> fixedArray<T, Input>(
  1. int size,
  2. BcsType<T, Input> type, [
  3. BcsTypeOptions<List<T>, Iterable<Input>>? options
])

Implementation

static BcsType<List<T>, Iterable<Input>> fixedArray<T, Input>(
  int size,
  BcsType<T, Input> type,
  [BcsTypeOptions<List<T>, Iterable<Input>>? options]
) {
  return BcsType<List<T>, Iterable<Input>>(
    name: '${type.name}[$size]',
    read: (reader) {
      final result = <T>[];
      for (int i = 0; i < size; i++) {
        result.add(type.read(reader));
      }
      return result;
    },
    write: (value, writer) {
      for (final item in value) {
        type.write(item, writer);
      }
    },
    validate: (value) {
      options?.validate?.call(value);
      if (value.length != size) {
        throw ArgumentError('Expected Iterable of length $size, found ${value.length}');
      }
    },
  );
}