Skip to content

Flutter runtime errorFlutter: “No MediaQuery widget ancestor found” / “MediaQuery.of() called with a context that does not contain a MediaQuery”

Flutter throws No MediaQuery widget ancestor found (older versions said MediaQuery.of() called with a context that does not contain a MediaQuery). Code asked for screen size, padding or text scale with a context that has no MediaQuery above it. Before Flutter 3.10 the classic cases were MediaQuery.of(context) in the build method that creates MaterialApp, and a widget test without MaterialApp. From 3.10 on, runApp and widget tests wrap everything in a View that provides a MediaQuery, so on current Flutter the error is rare and means the context is outside that View.

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

  1. 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);
        },
      ),
    );
  2. 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()),
      ),
    );
  3. 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.

Related: Flutter “Navigator operation requested with a context” · Flutter “A RenderFlex overflowed” · Flutter “Binding has not yet been initialized” · Dart “Bad state: No element”

Questions

More on this error

Should I use MediaQuery.of or MediaQuery.sizeOf?

sizeOf, paddingOf and the other ...Of methods when you need one value. Flutter’s docs say they rebuild your widget only when that value changes, while MediaQuery.of rebuilds on any change.

It worked before I moved code into main(). Why?

main() runs before any widget exists, so there is no context with a MediaQuery yet. Read screen values inside a widget’s build method instead.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. 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.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.