flutter encoding_error ai_generated true

FormatException: Invalid UTF-8 byte sequence

ID: flutter/formatexception-invalid-utf8-byte-sequence

Also available as: JSON · Markdown · 中文
85%Fix Rate
82%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Flutter 3.7 active
Flutter 3.10 active
Flutter 3.13 active

Root Cause

Data received from network or file is not valid UTF-8 encoded, causing Dart's utf8 decoder to fail.

generic

中文

从网络或文件接收的数据不是有效的 UTF-8 编码,导致 Dart 的 utf8 解码器失败。

Official Documentation

https://api.dart.dev/stable/dart-convert/Utf8Decoder-class.html

Workarounds

  1. 88% success Detect encoding first using package like 'charset' or 'universal_io' and decode with correct encoding: import 'dart:convert'; import 'package:charset/charset.dart'; List<int> bytes = ...; String text; try { text = utf8.decode(bytes); } on FormatException { text = latin1.decode(bytes); // fallback }
    Detect encoding first using package like 'charset' or 'universal_io' and decode with correct encoding:
    import 'dart:convert';
    import 'package:charset/charset.dart';
    
    List<int> bytes = ...;
    String text;
    try {
      text = utf8.decode(bytes);
    } on FormatException {
      text = latin1.decode(bytes); // fallback
    }
  2. 85% success Use utf8.decode(bytes, allowMalformed: true) and then sanitize or log replacement characters for debugging: String text = utf8.decode(bytes, allowMalformed: true); if (text.contains('\uFFFD')) { print('Malformed data detected'); }
    Use utf8.decode(bytes, allowMalformed: true) and then sanitize or log replacement characters for debugging:
    String text = utf8.decode(bytes, allowMalformed: true);
    if (text.contains('\uFFFD')) { print('Malformed data detected'); }
  3. 90% success Before decoding, validate bytes with a BOM (Byte Order Mark) or use a library like 'chardet' to auto-detect encoding.
    Before decoding, validate bytes with a BOM (Byte Order Mark) or use a library like 'chardet' to auto-detect encoding.

中文步骤

  1. Detect encoding first using package like 'charset' or 'universal_io' and decode with correct encoding:
    import 'dart:convert';
    import 'package:charset/charset.dart';
    
    List<int> bytes = ...;
    String text;
    try {
      text = utf8.decode(bytes);
    } on FormatException {
      text = latin1.decode(bytes); // fallback
    }
  2. Use utf8.decode(bytes, allowMalformed: true) and then sanitize or log replacement characters for debugging:
    String text = utf8.decode(bytes, allowMalformed: true);
    if (text.contains('\uFFFD')) { print('Malformed data detected'); }
  3. Before decoding, validate bytes with a BOM (Byte Order Mark) or use a library like 'chardet' to auto-detect encoding.

Dead Ends

Common approaches that don't work:

  1. Ignoring the error and assuming data is always valid UTF-8 90% fail

    The error will reappear whenever malformed data is encountered; no resilience.

  2. Using utf8.decode(bytes, allowMalformed: true) without logging 30% fail

    Silently replaces invalid sequences with replacement characters, potentially corrupting data silently.

  3. Assuming the data source will always send correct UTF-8 70% fail

    External data sources (especially user input or legacy systems) often send non-UTF-8 encodings like Latin-1.