writeString method

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

Writes text into a buffer field at address + elementOffset*4. If maxLength is null, it's derived from the UTF-32 code unit (rune) length of text (caller is responsible for the field actually being that size).

maxLength is in UTF-32 code units (runes), not bytes. Truncates if text exceeds maxLength; otherwise NUL-terminates and zero-pads the remainder.

Implementation

void writeString(String text, [int? maxLength, int elementOffset = 0]) {
  final runes = text.runes.toList(); // full scalar values, no surrogates
  final len = maxLength ?? runes.length;
  final writeLen = runes.length < len ? runes.length : len;
  final dst = offsetBy(elementOffset * 4).asView<Uint32List>(len);
  dst.setRange(0, writeLen, runes);
  dst.fillRange(writeLen, len, 0);
}