decodeFloat function

double decodeFloat(
  1. List<int> buffer,
  2. int offset
)

Decodes 4 little-endian bytes from buffer at offset as an IEEE 754 single-precision float.

Throws a RangeError if fewer than 4 bytes are available at offset.

Implementation

double decodeFloat(List<int> buffer, int offset) {
  if (offset + 4 > buffer.length) {
    throw RangeError(
      'Not enough bytes to decode float at offset $offset '
      '(need 4, have ${buffer.length - offset})',
    );
  }
  final bd = ByteData(4);
  bd.setUint8(0, buffer[offset]);
  bd.setUint8(1, buffer[offset + 1]);
  bd.setUint8(2, buffer[offset + 2]);
  bd.setUint8(3, buffer[offset + 3]);
  return bd.getFloat32(0, Endian.little);
}