# FormatException: JSON 中包含无效的 UTF-8 字节序列

- **ID:** `flutter/formatexception-invalid-json-utf8`
- **领域:** flutter
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Dart 3.0 | active | — | — |
| Dart 3.2 | active | — | — |
| Flutter 3.16 | active | — | — |

## 解决方案

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.
   ```

## 无效尝试

- **** — Silently drops data; the app continues with incomplete information, potentially causing downstream null errors or logic bugs. (70% 失败率)
- **** — utf8.encode expects valid strings; if the input is already corrupted, it will throw the same error or produce garbage. (85% 失败率)
- **** — Does not fix the underlying encoding issue; if the data is fundamentally corrupted, retrying will produce the same error. (65% 失败率)
