|

|  No MediaQuery widget ancestor found in Flutter: Causes and How to Fix

No MediaQuery widget ancestor found in Flutter: Causes and How to Fix

February 10, 2025

Discover the causes of the 'No MediaQuery widget ancestor found' error in Flutter and learn how to fix it effectively in this comprehensive guide.

What is No MediaQuery widget ancestor found Error in Flutter

 

No MediaQuery Widget Ancestor Found Error

 

The "No MediaQuery widget ancestor found" error in Flutter occurs when a widget tries to access media-related properties, such as size constraints or orientation, without being wrapped inside a MediaQuery widget. Flutter relies heavily on widget-based design, and the MediaQuery is a pivotal widget that provides information about the device's screen size and orientation to its descendants.

 

  • Flutter uses the `MediaQuery` widget to propagate media-related properties down the tree. It provides essential information like device dimensions, text scaling factors, and padding details, which are useful for responsive design.
  •  

  • This error commonly arises when widgets that inherently rely on `MediaQuery` information, such as `LayoutBuilder`, `OrientationBuilder`, or certain custom widgets, are not under a `MaterialApp`, `CupertinoApp`, or any widget tree providing a `MediaQuery` widget.

 

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // This widget can access MediaQuery because it's a descendant of MaterialApp
    final mediaQueryData = MediaQuery.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: Text(
          'Screen Width: ${mediaQueryData.size.width}',
        ),
      ),
    );
  }
}

 

  • In the example above, the `HomeScreen` widget successfully accesses `MediaQuery` data as it is enclosed within a `MaterialApp` widget. Any attempt to access `MediaQuery` outside such a hierarchy would lead to a "No MediaQuery widget ancestor found" error.
  •  

  • Understanding the error helps in structuring your Flutter widget tree efficiently, ensuring that widgets accessing media details are enclosed correctly.

 

What Causes No MediaQuery widget ancestor found in Flutter

 

Causes of "No MediaQuery Widget Ancestor Found" in Flutter

 

  • Incorrect Placement: The most common cause is placing a widget that depends on `MediaQuery` outside of its context scope, where no `MediaQuery` widget is an ancestor. This typically happens when you try to use `MediaQuery` in your widget tree before the widget tree has been attached to the app's widget tree.
  •  

  • Missing Scaffold: Flutter's `Scaffold` widget typically includes a `MediaQuery` ancestor. If you attempt to access `MediaQuery` data without a `Scaffold` or a similar widget higher up its hierarchy that creates a `MediaQuery` (like `MaterialApp`), it leads to this error.
  •  

  • Early Widget Access: Accessing `MediaQuery` in the `initState` of a stateful widget is a risky move because the widget tree is not yet fully built. The associated context might not have a `MediaQuery` ancestor at this stage.
  •  

  • Custom Contexts: When navigating between different contexts without ensuring that the new context provides a `MediaQuery`, developers can inadvertently attempt to read `MediaQuery` data.
  •  

  • Nested Context Use: When using a new `BuildContext` within an existing widget, such as in a `Builder` or `LayoutBuilder`, one might access `MediaQuery` incorrectly if the containing widget doesn't provide a clear context path back to a `MediaQuery` ancestor.

 


// Example causing "No MediaQuery widget ancestor found"
void main() {
  // No MaterialApp or WidgetsApp here
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // This will cause the error because there's no MediaQuery
    final width = MediaQuery.of(context).size.width; 
    return Container();
  }
}

 

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

How to Fix No MediaQuery widget ancestor found in Flutter

 

Wrap with a MaterialApp or WidgetsApp

 

  • To use MediaQuery, your widget tree should be wrapped with a MaterialApp or WidgetsApp. These widgets provide essential services for your application, including the MediaQuery.
  •  

  • If you're directly placing your widget as a root without wrapping it, you will encounter the "No MediaQuery widget ancestor found" error.

 

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

 

Use Builder Widget

 

  • If you need a MediaQuery and are inside a context where it's not available, consider using a Builder widget. This gives you a new context in which MediaQuery will be available.
  •  

  • This is especially useful when you're inside a Scaffold and need to access MediaQuery properties, like padding.

 

Scaffold(
  body: Builder(
    builder: (BuildContext context) {
      var mediaQuery = MediaQuery.of(context);
      return Text('Width: ${mediaQuery.size.width}');
    },
  ),
)

 

Place Widgets Correctly in the Tree

 

  • Make sure that your custom widgets that rely on MediaQuery are not placed before a MaterialApp or WidgetsApp. Place such widgets within the MaterialApp's child tree.
  •  

  • Avoid placing widgets that use MediaQuery directly in the initState or build method of a StatelessWidget outside the framework's widget tree context.

 

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: Text('Screen Width: ${mediaQuery.size.width}'),
      ),
    );
  }
}

 

Check Use of MediaQuery.of

 

  • Ensure you're calling MediaQuery.of(context) within a method that provides a valid context accessible to a MediaQuery. If called in invalid contexts, it results in the error.
  •  

  • Consider creating helper methods or classes if you're accessing MediaQuery in multiple places to avoid structural errors.

 

double deviceWidth(BuildContext context) {
  return MediaQuery.of(context).size.width;
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Device Width: ${deviceWidth(context)}'),
      ),
    );
  }
}

 

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Order Friend Dev Kit

Open-source AI wearable
Build using the power of recall

Order Now

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi Dev Kit 2

Endless customization

OMI DEV KIT 2

$69.99

Make your life more fun with your AI wearable clone. It gives you thoughts, personalized feedback and becomes your second brain to discuss your thoughts and feelings. Available on iOS and Android.

Your Omi will seamlessly sync with your existing omi persona, giving you a full clone of yourself – with limitless potential for use cases:

  • Real-time conversation transcription and processing;
  • Develop your own use cases for fun and productivity;
  • Hundreds of community apps to make use of your Omi Persona and conversations.

Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

thought to action

team@basedhardware.com

company

careers

invest

privacy

events

products

omi

omi dev kit

omiGPT

personas

omi glass

resources

apps

bounties

affiliate

docs

github

help