tryEncode static method

List<int>? tryEncode(
  1. String? value, {
  2. StringEncoding type = StringEncoding.utf8,
})

Encodes the given value string into a list of bytes using the specified type if possible.

The type parameter determines the encoding type to use, with UTF-8 being the default. Returns a list of bytes representing the encoded string.

Implementation

static List<int>? tryEncode(String? value,
    {StringEncoding type = StringEncoding.utf8}) {
  if (value == null) return null;
  try {
    return encode(value, type: type);
  } catch (e) {
    return null;
  }
}