flutter data_error ai_generated partial

FormatException: Invalid UTF-8 byte sequence in JSON

ID: flutter/formatexception-invalid-json-utf8

Also available as: JSON · Markdown · 中文
75%Fix Rate
82%Confidence
1Evidence
2024-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Dart 3.0 active
Dart 3.2 active
Flutter 3.16 active

Root Cause

A JSON response or file contains malformed UTF-8 bytes that Dart's utf8 decoder cannot parse, often due to corrupted data or encoding mismatch.

generic

中文

JSON 响应或文件包含 Dart 的 utf8 解码器无法解析的损坏 UTF-8 字节,通常是由于数据损坏或编码不匹配。

Official Documentation

https://api.dart.dev/stable/dart-convert/utf8-decode.html

Workarounds

  1. 80% success Use a forgiving UTF-8 decoder that replaces invalid sequences, such as utf8.decode with allowMalformed: true, then parse the cleaned string as JSON.
    Use a forgiving UTF-8 decoder that replaces invalid sequences, such as utf8.decode with allowMalformed: true, then parse the cleaned string as JSON.
  2. 70% success Validate and sanitize the input at the network layer: log the raw bytes, then request the server to fix the encoding or use a fallback endpoint.
    Validate and sanitize the input at the network layer: log the raw bytes, then request the server to fix the encoding or use a fallback endpoint.

中文步骤

  1. Use a forgiving UTF-8 decoder that replaces invalid sequences, such as utf8.decode with allowMalformed: true, then parse the cleaned string as JSON.
  2. Validate and sanitize the input at the network layer: log the raw bytes, then request the server to fix the encoding or use a fallback endpoint.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Silently drops data; the app continues with incomplete information, potentially causing downstream null errors or logic bugs.

  2. 85% fail

    utf8.encode expects valid strings; if the input is already corrupted, it will throw the same error or produce garbage.

  3. 65% fail

    Does not fix the underlying encoding issue; if the data is fundamentally corrupted, retrying will produce the same error.