# FormatException: 意外字符（位于字符 2）

- **ID:** `flutter/dart-format-exception-json-decode`
- **领域:** flutter
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

从网络请求或本地文件接收到的 JSON 字符串无效，可能被截断或格式错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Flutter 3.22.0 | active | — | — |
| Dart 3.4.0 | active | — | — |
| http 1.2.1 | active | — | — |
| dio 5.4.0 | active | — | — |

## 解决方案

1. ```
   Log the raw response body before decoding to inspect the actual content: `print(response.body);` and check for truncation or extra characters.
   ```
2. ```
   Use a JSON preprocessor to remove BOM or control characters: `String cleaned = response.body.replaceAll(RegExp(r'[\x00-\x1F\x7F]'), '');` then decode.
   ```
3. ```
   Add a retry mechanism with exponential backoff for network requests to handle transient truncation.
   ```

## 无效尝试

- **** — Ignoring the error leaves the app in an inconsistent state with null data, causing downstream crashes. (90% 失败率)
- **** — The format exception is raised by Dart's standard decoder; other libraries use the same parser. (95% 失败率)
- **** — The error is about JSON syntax, not character encoding; changing encoding may worsen the issue. (85% 失败率)
