{
  "id": "flutter/state-error-bloc-event-handler",
  "signature": "StateError: Bad state: Cannot add event while adding another event",
  "signature_zh": "状态错误：在添加另一个事件时无法添加事件",
  "regex": "StateError: Bad state: Cannot add event while adding another event",
  "domain": "flutter",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Attempting to call bloc.add() from within an event handler of the same Bloc instance, causing a reentrant call.",
  "root_cause_type": "generic",
  "root_cause_zh": "在同一个 Bloc 实例的事件处理程序中调用 bloc.add()，导致重入调用。",
  "versions": [
    {
      "version": "flutter 3.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "flutter 3.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "bloc 8.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "flutter_bloc 8.1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding a short delay with Future.delayed before bloc.add()",
      "why_fails": "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.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Wrapping bloc.add() in a try-catch block",
      "why_fails": "The StateError is thrown synchronously and cannot be caught inside the event handler because Bloc internally throws it before the catch block is reached.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use a separate isolate or schedule the new event outside the current handler using Future.microtask(() => bloc.add(Event())). This ensures the current event processing completes before the new event is added.",
      "success_rate": 0.9,
      "how": "Use a separate isolate or schedule the new event outside the current handler using Future.microtask(() => bloc.add(Event())). This ensures the current event processing completes before the new event is added.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Restructure the Bloc logic to combine both operations into a single event or use async gaps like await Future.delayed(Duration.zero) before calling bloc.add().",
      "success_rate": 0.85,
      "how": "Restructure the Bloc logic to combine both operations into a single event or use async gaps like await Future.delayed(Duration.zero) before calling bloc.add().",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "使用单独的隔离或通过 Future.microtask(() => bloc.add(Event())) 在当前处理程序外部调度新事件。这确保当前事件处理完成后再添加新事件。",
    "重构 Bloc 逻辑，将两个操作合并为一个事件，或在使用 bloc.add() 之前使用异步间隙，如 await Future.delayed(Duration.zero)。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://bloclibrary.dev/#/coreconcepts?id=adding-events",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-11-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}