encode static method

Future<Uint8List> encode(
  1. String charset,
  2. String data
)

Encodes data to given charset supported by the platform.

When encoding fails either CharsetConversionError or PlatformException is thrown.

Implementation

static Future<Uint8List> encode(String charset, String data) async {
  late final Uint8List? result;

  if (Platform.isLinux) {
    // A bit silly, but GLib string (gchar *) is the same as C's, the end of the string must be '\0'.
    // We are bad at C so we handle it here.
    result = await _channel.invokeMethod('encode', {
      "charset": charset,
      "data": Uint8List.fromList([...utf8.encode(data), 0]),
    });
  } else {
    result = await _channel.invokeMethod('encode', {
      "charset": charset,
      "data": data,
    });
  }

  if (result == null) {
    throw CharsetConversionError(
      "Result of encoding was null which may indicate that native converter failed to encode your data",
      true,
    );
  }

  return result;
}