# 状态错误：在添加另一个事件时无法添加事件

- **ID:** `flutter/state-error-bloc-event-handler`
- **领域:** flutter
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

在同一个 Bloc 实例的事件处理程序中调用 bloc.add()，导致重入调用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| flutter 3.16 | active | — | — |
| flutter 3.22 | active | — | — |
| bloc 8.0.0 | active | — | — |
| flutter_bloc 8.1.0 | active | — | — |

## 解决方案

1. ```
   使用单独的隔离或通过 Future.microtask(() => bloc.add(Event())) 在当前处理程序外部调度新事件。这确保当前事件处理完成后再添加新事件。
   ```
2. ```
   重构 Bloc 逻辑，将两个操作合并为一个事件，或在使用 bloc.add() 之前使用异步间隙，如 await Future.delayed(Duration.zero)。
   ```

## 无效尝试

- **Adding a short delay with Future.delayed before bloc.add()** — Delay does not prevent the reentrant call from the same synchronous context; the error still occurs if the logic is in the same event handler chain. (85% 失败率)
- **Wrapping bloc.add() in a try-catch block** — The StateError is thrown synchronously and cannot be caught inside the event handler because Bloc internally throws it before the catch block is reached. (75% 失败率)
