toUtf8 method

Pointer<Utf8> toUtf8(
  1. String string

Convert a String to a Utf8-encoded null-terminated C string.

If 'string' contains NULL bytes, the converted string will be truncated prematurely. Unpaired surrogate code points in string will be preserved in the UTF-8 encoded result. See Utf8Encoder for details on encoding.

Returns a malloc-allocated pointer to the result.


static Pointer<Utf8> toUtf8(String string) {
  final units = utf8.encode(string);
  final Pointer<Uint8> result = allocate<Uint8>(count: units.length + 1);
  final Uint8List nativeString = result.asTypedList(units.length + 1);
  nativeString.setAll(0, units);
  nativeString[units.length] = 0;
  return result.cast();