writeString method

void writeString(
  1. String text, [
  2. int? maxLength,
  3. int byteOffset = 0
])

Writes text into a buffer field at address + byteOffset. If maxLength is null, it's derived from the UTF-8 byte length of text (caller is responsible for the field actually being that size). Truncates if text exceeds maxLength; otherwise NUL-terminates and zero-pads the remainder.

Implementation

void writeString(String text, [int? maxLength, int byteOffset = 0]) {
  final bytes = utf8.encode(text);
  final len = maxLength ?? bytes.length;
  final writeLen = bytes.length < len ? bytes.length : len;
  final dst = offsetBy(byteOffset).asView<Uint8List>(len);
  dst.setRange(0, writeLen, bytes);
  dst.fillRange(writeLen, len, 0);
}