validateSample method

void validateSample(
  1. Iterable data
)

Checks that data is a valid sample for this stream.

A sample is exactly NetworkStreamConfig.channels values whose runtime types match NetworkStreamConfig.dataType. Transport-neutral, so every backend rejects the same inputs with the same message instead of each discovering the mismatch in its own way (or not at all).

Throws ArgumentError describing the first problem found.

Implementation

void validateSample(Iterable<dynamic> data) {
  if (data.length != channels) {
    throw ArgumentError(
      'Data length ${data.length} does not match channels $channels '
      'on stream "$name"',
    );
  }
  for (final value in data) {
    switch (dataType) {
      case StreamDataType.float32:
      case StreamDataType.double64:
        if (value is! num) {
          throw ArgumentError(
            'Expected numeric value, got ${value.runtimeType}',
          );
        }
      case StreamDataType.int8:
      case StreamDataType.int16:
      case StreamDataType.int32:
      case StreamDataType.int64:
        if (value is! int) {
          throw ArgumentError('Expected int value, got ${value.runtimeType}');
        }
      case StreamDataType.string:
        if (value is! String) {
          throw ArgumentError(
            'Expected String value, got ${value.runtimeType}',
          );
        }
    }
  }
}