Source
Uint8List toUTF8(String str) {
int length = str.length;
Uint8List bytes = new Uint8List(length);
for (int i = 0; i < length; i++) {
int unit = str.codeUnitAt(i);
if (unit >= 128) {
return new Uint8List.fromList(const Utf8Encoder().convert(str));
}
bytes[i] = unit;
}
return bytes;
}