flutter
assertion_error
ai_generated
true
Assertion failed: MediaQuery.of() called with a context that does not contain a MediaQuery ancestor.
ID: flutter/assertion-error-media-query-before-run
92%Fix Rate
89%Confidence
1Evidence
2023-06-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| flutter 3.10 | active | — | — | — |
| flutter 3.22 | active | — | — | — |
| dart 3.0 | active | — | — | — |
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.
generic中文
MediaQuery.of(context) 在 widget 被插入到具有 MaterialApp 或 WidgetsApp 祖先(提供 MediaQuery)的 widget 树之前被调用,通常在 build 期间但在 runApp() 之前,或在一个不是 MaterialApp 后代的 widget 中。
Official Documentation
https://api.flutter.dev/flutter/widgets/MediaQuery/of.htmlWorkarounds
-
95% success 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.
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.
-
90% success Pass MediaQuery data as a parameter to widgets that need it, rather than calling MediaQuery.of(context) inside them.
Pass MediaQuery data as a parameter to widgets that need it, rather than calling MediaQuery.of(context) inside them.
中文步骤
确保 MediaQuery.of(context) 仅在 MaterialApp 或 WidgetsApp 后代的 build 方法中调用。如果在 initState 中需要,使用 WidgetsBinding.instance.addPostFrameCallback 延迟调用。
将 MediaQuery 数据作为参数传递给需要它的 widget,而不是在其中调用 MediaQuery.of(context)。
Dead Ends
Common approaches that don't work:
-
Adding MediaQuery.of(context) directly in main() before runApp()
100% fail
The context from runApp() is not available; MediaQuery is only set up after the MaterialApp widget is built.
-
Using a global variable to store MediaQuery data before the widget tree is built
90% fail
MediaQuery depends on the widget tree context; it cannot be accessed outside the build method or before the tree is established.