{
  "id": "flutter/setstate-or-initstate-async",
  "signature": "setState() or markNeedsBuild() called during build",
  "signature_zh": "在构建过程中调用了 setState() 或 markNeedsBuild()",
  "regex": "setState\\(\\) or markNeedsBuild\\(\\) called during build",
  "domain": "flutter",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A widget's build method or its dependencies triggered setState() synchronously, violating Flutter's single-frame build constraint.",
  "root_cause_type": "generic",
  "root_cause_zh": "Widget 的 build 方法或其依赖项同步触发了 setState()，违反了 Flutter 的单帧构建约束。",
  "versions": [
    {
      "version": "Flutter 3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Flutter 3.13",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Flutter 3.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Wrapping the setState() call in a Future.delayed(Duration.zero)",
      "why_fails": "Merely postpones the call to the next frame but does not address the root cause of calling setState during build.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "Moving setState() to initState() without checking lifecycle",
      "why_fails": "initState() is called before build, but if the setState depends on BuildContext or inherited widgets not yet available, it can still cause issues.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using a global variable to track build state and skip setState",
      "why_fails": "Brittle and error-prone; global state management often leads to other race conditions and missed updates.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use addPostFrameCallback to defer the setState call until after the current build frame:\nWidgetsBinding.instance.addPostFrameCallback((_) {\n  setState(() { /* update state */ });\n});",
      "success_rate": 0.95,
      "how": "Use addPostFrameCallback to defer the setState call until after the current build frame:\nWidgetsBinding.instance.addPostFrameCallback((_) {\n  setState(() { /* update state */ });\n});",
      "condition": "",
      "sources": []
    },
    {
      "action": "Restructure widget to avoid calling setState during build. Move the triggering logic to initState or a separate event handler that runs after the initial layout.",
      "success_rate": 0.85,
      "how": "Restructure widget to avoid calling setState during build. Move the triggering logic to initState or a separate event handler that runs after the initial layout.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the update is from a stream or future, use a StreamBuilder or FutureBuilder to handle asynchronous state changes without manual setState.",
      "success_rate": 0.9,
      "how": "If the update is from a stream or future, use a StreamBuilder or FutureBuilder to handle asynchronous state changes without manual setState.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use addPostFrameCallback to defer the setState call until after the current build frame:\nWidgetsBinding.instance.addPostFrameCallback((_) {\n  setState(() { /* update state */ });\n});",
    "Restructure widget to avoid calling setState during build. Move the triggering logic to initState or a separate event handler that runs after the initial layout.",
    "If the update is from a stream or future, use a StreamBuilder or FutureBuilder to handle asynchronous state changes without manual setState."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://api.flutter.dev/flutter/widgets/State/setState.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-08-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}