flutter
data_error
ai_generated
partial
FormatException: JSON 中包含无效的 UTF-8 字节序列
FormatException: Invalid UTF-8 byte sequence in JSON
ID: flutter/formatexception-invalid-json-utf8
75%修复率
82%置信度
1证据数
2024-01-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Dart 3.0 | active | — | — | — |
| Dart 3.2 | active | — | — | — |
| Flutter 3.16 | active | — | — | — |
根因分析
JSON 响应或文件包含 Dart 的 utf8 解码器无法解析的损坏 UTF-8 字节,通常是由于数据损坏或编码不匹配。
English
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.
官方文档
https://api.dart.dev/stable/dart-convert/utf8-decode.html解决方案
-
Use a forgiving UTF-8 decoder that replaces invalid sequences, such as utf8.decode with allowMalformed: true, then parse the cleaned string as JSON.
-
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.
无效尝试
常见但无效的做法:
-
70% 失败
Silently drops data; the app continues with incomplete information, potentially causing downstream null errors or logic bugs.
-
85% 失败
utf8.encode expects valid strings; if the input is already corrupted, it will throw the same error or produce garbage.
-
65% 失败
Does not fix the underlying encoding issue; if the data is fundamentally corrupted, retrying will produce the same error.