|

|  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 開発キット 2

無限のカスタマイズ

OMI 開発キット 2

$69.99

Omi AIネックレスで会話を音声化、文字起こし、要約。アクションリストやパーソナライズされたフィードバックを提供し、あなたの第二の脳となって考えや感情を語り合います。iOSとAndroidでご利用いただけます。

  • リアルタイムの会話の書き起こしと処理。
  • 行動項目、要約、思い出
  • Omi ペルソナと会話を活用できる何千ものコミュニティ アプリ

もっと詳しく知る

Omi Dev Kit 2: 新しいレベルのビルド

主な仕様

OMI 開発キット

OMI 開発キット 2

マイクロフォン

はい

はい

バッテリー

4日間(250mAH)

2日間(250mAH)

オンボードメモリ(携帯電話なしで動作)

いいえ

はい

スピーカー

いいえ

はい

プログラム可能なボタン

いいえ

はい

配送予定日

-

1週間

人々が言うこと

「記憶を助ける、

コミュニケーション

ビジネス/人生のパートナーと、

アイデアを捉え、解決する

聴覚チャレンジ」

ネイサン・サッズ

「このデバイスがあればいいのに

去年の夏

記録する

「会話」

クリスY.

「ADHDを治して

私を助けてくれた

整頓された。"

デビッド・ナイ

OMIネックレス:開発キット
脳を次のレベルへ

最新ニュース
フォローして最新情報をいち早く入手しましょう

最新ニュース
フォローして最新情報をいち早く入手しましょう

thought to action.

Based Hardware Inc.
81 Lafayette St, San Francisco, CA 94103
team@basedhardware.com / help@omi.me

Company

Careers

Invest

Privacy

Events

Manifesto

Compliance

Products

Omi

Wrist Band

Omi Apps

omi Dev Kit

omiGPT

Personas

Omi Glass

Resources

Apps

Bounties

Affiliate

Docs

GitHub

Help Center

Feedback

Enterprise

Ambassadors

Resellers

© 2025 Based Hardware. All rights reserved.