Skip to content

No MediaQuery ancestor could be found with MaterialApp available #88601

@skiunke

Description

@skiunke

Steps to Reproduce

  1. Run flutter create bug.

2.Create a basic StatlessWidget

import 'package:flutter/material.dart';

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

class Bug extends StatelessWidget {
  const Bug({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Container(
            height: MediaQuery.of(context).size.height * 0.50,
          ),
        ),
      ),
    );
  }
}

Expected results:
Container with size of 50% of devices height

Actual results:
Exception: No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.

Logs
flutter run -d chrome --verbose
[  +47 ms] executing: uname -m
[  +22 ms] Exit code 0 from: uname -m
[        ] x86_64
[   +4 ms] executing: [/home/seby/snap/flutter/common/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[   +3 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[        ] 02c026b03cd31dd3f867e5faeb7e104cce174c5f
[        ] executing: [/home/seby/snap/flutter/common/flutter/] git tag --points-at 02c026b03cd31dd3f867e5faeb7e104cce174c5f
[   +8 ms] Exit code 0 from: git tag --points-at 02c026b03cd31dd3f867e5faeb7e104cce174c5f
[        ] 2.2.1
[   +4 ms] executing: [/home/seby/snap/flutter/common/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[   +3 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/stable
[        ] executing: [/home/seby/snap/flutter/common/flutter/] git ls-remote --get-url origin
[   +2 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[  +57 ms] executing: [/home/seby/snap/flutter/common/flutter/] git rev-parse --abbrev-ref HEAD
[   +3 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] stable
[  +38 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +1 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +40 ms] executing: /home/seby/Android/Sdk/platform-tools/adb devices -l
[  +11 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +40 ms] executing: /home/seby/Android/Sdk/platform-tools/adb devices -l
[   +7 ms] Skipping pub get: version match.
[  +79 ms] Generating /home/seby/bug/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
[  +60 ms] List of devices attached
           emulator-5554          device product:sdk_phone_x86 model:Android_SDK_built_for_x86 device:generic_x86 transport_id:2
[   +3 ms] List of devices attached
           emulator-5554          device product:sdk_phone_x86 model:Android_SDK_built_for_x86 device:generic_x86 transport_id:2
[  +37 ms] This application is not configured to build on the web.
[        ] To add web support to a project, run `flutter create .`.
[        ] Launching lib/main.dart on Chrome in debug mode...
[  +67 ms] Updating assets
[  +53 ms] Waiting for connection from debug service on Chrome...
[  +70 ms] <- reset
[   +3 ms] /home/seby/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart --disable-dart-dev /home/seby/snap/flutter/common/flutter/bin/cache/artifacts/engine/linux-x64/frontend_server.dart.snapshot --sdk-root /home/seby/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/ --incremental --target=dartdevc
--debugger-module-names --experimental-emit-debug-metadata -DFLUTTER_WEB_AUTO_DETECT=true --output-dill /tmp/flutter_tools.ZZUWRX/flutter_tool.NUINBB/app.dill --libraries-spec file:///home/seby/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/libraries.json --packages
/home/seby/bug/.dart_tool/package_config.json -Ddart.vm.profile=false -Ddart.vm.product=false --enable-asserts --track-widget-creation --filesystem-root /tmp/flutter_tools.ZZUWRX/flutter_tools.GJVMJX --filesystem-scheme org-dartlang-app --initialize-from-dill
build/b1b715402d823b7fd5c2b68d2edcb2ce.cache.dill.track.dill --platform file:///home/seby/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/kernel/flutter_ddc_sdk_sound.dill --sound-null-safety
[   +7 ms] <- compile org-dartlang-app:/web_entrypoint.dart
[+12996 ms] Waiting for connection from debug service on Chrome... (completed in 13.1s)
[        ] Synced 28.2MB.
[        ] <- accept
[        ] Caching compiled dill
[ +122 ms] Using Google Chrome 92.0.4515.159 

[ +227 ms] [CHROME]:
[        ] [CHROME]:DevTools listening on ws://127.0.0.1:32981/devtools/browser/88a26062-8d0d-4311-a43d-52442550168b
[ +202 ms] DwdsInjector: Received request for entrypoint at http://localhost:45295/main_module.bootstrap.js
[   +3 ms] MetadataProvider: Loading debug metadata...
[   +9 ms] MetadataProvider: Loaded debug metadata (sound null safety)
[   +4 ms] DwdsInjector: Injected debugging metadata for entrypoint at http://localhost:45295/main_module.bootstrap.js
[+1201 ms] ChromeProxyService: Initializing expression compiler for main_module.bootstrap.js with sound null safety: true
[  +48 ms] DevHandler: Debug service listening on ws://127.0.0.1:39737/NjobN7PY60U=/ws

[  +16 ms] This app is linked to the debug service: ws://127.0.0.1:39737/NjobN7PY60U=/ws
[   +5 ms] Debug service listening on ws://127.0.0.1:39737/NjobN7PY60U=/ws
[        ] 💪 Running with sound null safety 💪
[   +2 ms] 🔥  To hot restart changes while running, press "r" or "R".
[        ] For a more detailed help message, press "h". To quit, press "q".
[ +891 ms] ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
[        ] The following assertion was thrown building Bug(dirty):
[        ] No MediaQuery widget ancestor found.
[        ] Bug widgets require a MediaQuery widget ancestor.
[  +13 ms] The specific widget that could not find a MediaQuery ancestor was:
[        ]   Bug
[        ] The ownership chain for the affected widget is: "Bug ← [root]"
[        ] No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
[        ] This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those
[        ] widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above
[        ] those widgets.
[        ] The relevant error-causing widget was:
[        ]   Bug file:///home/seby/bug/lib/main.dart:4:10
[        ] When the exception was thrown, this was the stack:
[        ] dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49  throw_
[        ] packages/flutter/src/widgets/debug.dart 219:7                                 <fn>
[        ] packages/flutter/src/widgets/debug.dart 233:12                                debugCheckHasMediaQuery
[        ] packages/flutter/src/widgets/media_query.dart 820:12                          of
[        ] packages/bug/main.dart 12:113                                                 build
[        ] packages/flutter/src/widgets/framework.dart 4648:28                           build
[        ] packages/flutter/src/widgets/framework.dart 4574:15                           performRebuild
[        ] packages/flutter/src/widgets/framework.dart 4267:5                            rebuild
[   +2 ms] packages/flutter/src/widgets/framework.dart 4553:5                            [_firstBuild]
[        ] packages/flutter/src/widgets/framework.dart 4548:5                            mount
[        ] packages/flutter/src/widgets/framework.dart 3611:13                           inflateWidget
[        ] packages/flutter/src/widgets/framework.dart 3363:18                           updateChild
[        ] packages/flutter/src/widgets/binding.dart 1189:16                             [_rebuild]
[        ] packages/flutter/src/widgets/binding.dart 1159:5                              mount
[        ] packages/flutter/src/widgets/binding.dart 1104:16                             <fn>
[        ] packages/flutter/src/widgets/framework.dart 2535:19                           buildScope
[        ] packages/flutter/src/widgets/binding.dart 1103:12                             attachToRenderTree
[        ] packages/flutter/src/widgets/binding.dart 936:24                              attachRootWidget
[        ] packages/flutter/src/widgets/binding.dart 917:7                               <fn>
[        ] dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:19       internalCallback
[        ] ════════════════════════════════════════════════════════════════════════════════════════════════════


No issues found
[✓] Flutter (Channel stable, 2.2.1, on Linux, locale en_US.UTF-8)
    • Flutter version 2.2.1 at /home/seby/snap/flutter/common/flutter
    • Framework revision 02c026b03c (3 months ago), 2021-05-27 12:24:44 -0700
    • Engine revision 0fdb562ac8
    • Dart version 2.13.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /home/seby/Android/Sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /home/seby/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/203.7621141/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /home/seby/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/203.7621141
    • Flutter plugin version 59.0.2
    • Dart plugin version 203.8292
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.2)
    • IntelliJ at /home/seby/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/212.4746.92
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] Connected device (2 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 92.0.4515.159

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: invalidIssue is closed as not valid

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions