decodeAsText static method

String decodeAsText(
  1. Uint8List data,
  2. String? transferEncoding,
  3. String? charset
)

Implementation

static String decodeAsText(
    final Uint8List data, String? transferEncoding, String? charset) {
  // there is actually just one interesting case:
  // when the transfer encoding is 8bit, the text needs to be decoded with the specifed charset.
  // Note that some mail senders also declare 7bit message encoding even when UTF8 or other 8bit encodings are used.
  // In other cases the text is ASCII and the 'normal' decodeAnyText method can be used.
  final transferEncodingLC = transferEncoding?.toLowerCase() ?? '8bit';
  if (transferEncodingLC == '8bit' || transferEncodingLC == '7bit') {
    charset ??= 'utf8';
    final codec = _charsetCodecsByName[charset.toLowerCase()];
    if (codec == null) {
      print('Error: no encoding found for charset [$charset].');
      return encodingUtf8.decode(data);
    }
    return codec.decode(data);
  }
  final text = String.fromCharCodes(data);
  return decodeAnyText(text, transferEncoding, charset);
}