bytesToString static method

String bytesToString(
  1. Uint8List bytes, [
  2. MimeType? mimeType
])

Converts bytes using the encoding specified by mimeType if provided. If mimeType is null or an Encoding cannot be determined, it will attempt to decode using UTF-8, and if that fails, it will try LATIN-1.

Implementation

static String bytesToString(Uint8List bytes, [MimeType? mimeType]) {
  if (mimeType != null) {
    if (mimeType.isCharsetUTF8) {
      return utf8.decode(bytes);
    } else if (mimeType.isCharsetLATIN1) {
      return utf8.decode(bytes);
    } else if (mimeType.isCharsetUTF16) {
      return utf16.decode(bytes);
    } else {
      var encoding = mimeType.preferredStringEncoding;
      if (encoding != null) {
        return encoding.decode(bytes);
      }
    }
  }

  try {
    return utf8.decode(bytes);
  } catch (_) {
    return latin1.decode(bytes);
  }
}