{
  "id": "flutter/assertion-error-media-query-before-run",
  "signature": "Assertion failed: MediaQuery.of() called with a context that does not contain a MediaQuery ancestor.",
  "signature_zh": "断言失败：MediaQuery.of() 使用的上下文不包含 MediaQuery 祖先。",
  "regex": "Assertion failed: MediaQuery.of\\(\\) called with a context that does not contain a MediaQuery ancestor",
  "domain": "flutter",
  "category": "assertion_error",
  "subcategory": null,
  "root_cause": "MediaQuery.of(context) is called before the widget is inserted into the widget tree that has a MaterialApp or WidgetsApp ancestor providing MediaQuery, typically during build before runApp() or in a widget that is not a descendant of MaterialApp.",
  "root_cause_type": "generic",
  "root_cause_zh": "MediaQuery.of(context) 在 widget 被插入到具有 MaterialApp 或 WidgetsApp 祖先（提供 MediaQuery）的 widget 树之前被调用，通常在 build 期间但在 runApp() 之前，或在一个不是 MaterialApp 后代的 widget 中。",
  "versions": [
    {
      "version": "flutter 3.10",
      "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": "dart 3.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding MediaQuery.of(context) directly in main() before runApp()",
      "why_fails": "The context from runApp() is not available; MediaQuery is only set up after the MaterialApp widget is built.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using a global variable to store MediaQuery data before the widget tree is built",
      "why_fails": "MediaQuery depends on the widget tree context; it cannot be accessed outside the build method or before the tree is established.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure MediaQuery.of(context) is only called inside a build method of a widget that is a descendant of MaterialApp or WidgetsApp. If needed in initState, use WidgetsBinding.instance.addPostFrameCallback to delay the call.",
      "success_rate": 0.95,
      "how": "Ensure MediaQuery.of(context) is only called inside a build method of a widget that is a descendant of MaterialApp or WidgetsApp. If needed in initState, use WidgetsBinding.instance.addPostFrameCallback to delay the call.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Pass MediaQuery data as a parameter to widgets that need it, rather than calling MediaQuery.of(context) inside them.",
      "success_rate": 0.9,
      "how": "Pass MediaQuery data as a parameter to widgets that need it, rather than calling MediaQuery.of(context) inside them.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "确保 MediaQuery.of(context) 仅在 MaterialApp 或 WidgetsApp 后代的 build 方法中调用。如果在 initState 中需要，使用 WidgetsBinding.instance.addPostFrameCallback 延迟调用。",
    "将 MediaQuery 数据作为参数传递给需要它的 widget，而不是在其中调用 MediaQuery.of(context)。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://api.flutter.dev/flutter/widgets/MediaQuery/of.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.89,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-06-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}