# Assertion failed: MediaQuery.of() called with a context that does not contain a MediaQuery ancestor.

- **ID:** `flutter/assertion-error-media-query-before-run`
- **Domain:** flutter
- **Category:** assertion_error
- **Verification:** ai_generated
- **Fix Rate:** 92%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| flutter 3.10 | active | — | — |
| flutter 3.22 | active | — | — |
| dart 3.0 | active | — | — |

## Workarounds

1. **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.** (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.
   ```
2. **Pass MediaQuery data as a parameter to widgets that need it, rather than calling MediaQuery.of(context) inside them.** (90% success)
   ```
   Pass MediaQuery data as a parameter to widgets that need it, rather than calling MediaQuery.of(context) inside them.
   ```

## Dead Ends

- **Adding MediaQuery.of(context) directly in main() before runApp()** — The context from runApp() is not available; MediaQuery is only set up after the MaterialApp widget is built. (100% fail)
- **Using a global variable to store MediaQuery data before the widget tree is built** — MediaQuery depends on the widget tree context; it cannot be accessed outside the build method or before the tree is established. (90% fail)
