Short answer
On Flutter before 3.10, read MediaQuery below MaterialApp (in the home screen, or inside a Builder) and wrap widgets under test in MaterialApp; upgrading Flutter removes both cases. On current Flutter, find the context that is outside a View: one from main() or a service class, a runWidget or multi-view setup without a View, or a test that passes wrapWithView: false. Pass the values in from a widget, or use MediaQuery.maybeSizeOf.
Why there is no MediaQuery, and the fix
01
Old Flutter: MediaQuery read in the widget that builds MaterialApp
“WidgetsApp and MaterialApp, which introduce a MediaQuery and keep it up to date with the current screen metrics as they change.” Flutter, MediaQuery class
Before 3.10 nothing above MaterialApp provided a MediaQuery, and the context passed to a build method belongs to the widget that returns MaterialApp. Use the size inside the home screen, or give the code a context below MaterialApp with a Builder. Upgrading Flutter also fixes it.
return MaterialApp( home: Builder( builder: (context) { final width = MediaQuery.sizeOf(context).width; // below MaterialApp return HomePage(compact: width < 600); }, ), );02
Old Flutter: a widget test without an app around the widget
Before 3.10, tester.pumpWidget added no MediaQuery. Wrap the widget under test in MaterialApp, which Material widgets need in tests anyway. On current Flutter, this error in a test means the test passed wrapWithView: false.
await tester.pumpWidget( const MaterialApp( home: Scaffold(body: ProfileCard()), ), );03
Current Flutter: a context outside the View
“This can happen because the context used is not a descendant of a View widget, which introduces a MediaQuery.” Flutter, Flutter widgets debug.dart
A context from main() or a service class, or a widget tree started with runWidget or a multi-view setup without a View, has no MediaQuery above it. Pass the values in from a widget that has one, or use MediaQuery.maybeSizeOf, which returns null instead of throwing.
final size = MediaQuery.maybeSizeOf(context); if (size == null) return const SizedBox.shrink();
Checked against the vendor’s docs on : Flutter API: MediaQuery class; Flutter source: debugCheckHasMediaQuery (widgets/debug.dart).
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.See scope and start
Related: Flutter “Navigator operation requested with a context” · Flutter “A RenderFlex overflowed” · Flutter “Binding has not yet been initialized” · Dart “Bad state: No element”