【错误记录】Flutter 界面报错 ( No MediaQuery widget ancestor found. | Scaffold widgets require a MediaQuery )
生活随笔
收集整理的這篇文章主要介紹了
【错误记录】Flutter 界面报错 ( No MediaQuery widget ancestor found. | Scaffold widgets require a MediaQuery )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、報錯信息
- 二、解決方案
一、報錯信息
Flutter 應用運行時報如下錯誤 :
Launching lib\main.dart on Pixel 2 in debug mode... Running Gradle task 'assembleDebug'... √ Built build\app\outputs\flutter-apk\app-debug.apk. Installing build\app\outputs\flutter-apk\app.apk... Uninstalling old version... Error: ADB exited with exit code 1 Performing Streamed Installadb: failed to install D:\002_Project\002_Android_Learn\flutter_animation\build\app\outputs\flutter-apk\app.apk: Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE: Package kim.hsl.flutter_animation signatures do not match previously installed version; ignoring!] Installing build\app\outputs\flutter-apk\app.apk... Debug service listening on ws://127.0.0.1:59548/20KwYgDmkUc=/ws Syncing files to device Pixel 2...======== Exception caught by widgets library ======================================================= The following assertion was thrown building HeroAnimation: No MediaQuery widget ancestor found.Scaffold widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: Scaffolddirtystate: ScaffoldState#6ef6f(lifecycle state: initialized, tickers: tracking 2 tickers) The ownership chain for the affected widget is: "Scaffold ← HeroAnimation ← [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: HeroAnimation file:///D:/002_Project/002_Android_Learn/flutter_animation/lib/main.dart:5:10 When the exception was thrown, this was the stack: #0 debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:219:7) #1 debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:234:4) #2 MediaQuery.of (package:flutter/src/widgets/media_query.dart:820:12) #3 ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:2820:50) #4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4725:11) ... ====================================================================================================出錯代碼 :
void main() {runApp(HeroAnimation()); }class HeroAnimation extends StatelessWidget{@overrideWidget build(BuildContext context) {// 時間膨脹系數 , 用于降低動畫運行速度timeDilation = 10.0;return Scaffold(appBar: AppBar(title: Text("Hero 動畫演示( 跳轉前頁面 )"),),body: Center(child: HeroWidget(imageUrl: "https://img-blog.csdnimg.cn/20210329101628636.jpg",width: 300,),),);} }二、解決方案
出現上述問題 , 是因為在界面的根組件 , 沒有使用 MaterialApp 組件 , 在 main.dart 中的 main 函數中 , 運行的組件的根組件必須是 MaterialApp ;
在 Scaffold 組件的外層包裹一層 MaterialApp , 即可解決問題 ;
class HeroAnimation extends StatelessWidget{@overrideWidget build(BuildContext context) {// 時間膨脹系數 , 用于降低動畫運行速度timeDilation = 10.0;return MaterialApp(home: Scaffold(appBar: AppBar(title: Text("Hero 動畫演示( 跳轉前頁面 )"),),body: ,),);}}總結
以上是生活随笔為你收集整理的【错误记录】Flutter 界面报错 ( No MediaQuery widget ancestor found. | Scaffold widgets require a MediaQuery )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flutter】Animation 动
- 下一篇: 【错误记录】Flutter 界面跳转报错