【错误记录】Flutter 界面报错 ( No MediaQuery widget ancestor found. | Scaffold widgets require a MediaQuery )
生活随笔
收集整理的這篇文章主要介紹了
【错误记录】Flutter 界面报错 ( No MediaQuery widget ancestor found. | Scaffold widgets require a MediaQuery )
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、報(bào)錯(cuò)信息
- 二、解決方案
一、報(bào)錯(cuò)信息
Flutter 應(yīng)用運(yùn)行時(shí)報(bào)如下錯(cuò)誤 :
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) ... ====================================================================================================出錯(cuò)代碼 :
void main() {runApp(HeroAnimation()); }class HeroAnimation extends StatelessWidget{@overrideWidget build(BuildContext context) {// 時(shí)間膨脹系數(shù) , 用于降低動(dòng)畫(huà)運(yùn)行速度timeDilation = 10.0;return Scaffold(appBar: AppBar(title: Text("Hero 動(dòng)畫(huà)演示( 跳轉(zhuǎn)前頁(yè)面 )"),),body: Center(child: HeroWidget(imageUrl: "https://img-blog.csdnimg.cn/20210329101628636.jpg",width: 300,),),);} }二、解決方案
出現(xiàn)上述問(wèn)題 , 是因?yàn)樵诮缑娴母M件 , 沒(méi)有使用 MaterialApp 組件 , 在 main.dart 中的 main 函數(shù)中 , 運(yùn)行的組件的根組件必須是 MaterialApp ;
在 Scaffold 組件的外層包裹一層 MaterialApp , 即可解決問(wèn)題 ;
class HeroAnimation extends StatelessWidget{@overrideWidget build(BuildContext context) {// 時(shí)間膨脹系數(shù) , 用于降低動(dòng)畫(huà)運(yùn)行速度timeDilation = 10.0;return MaterialApp(home: Scaffold(appBar: AppBar(title: Text("Hero 動(dòng)畫(huà)演示( 跳轉(zhuǎn)前頁(yè)面 )"),),body: ,),);}}總結(jié)
以上是生活随笔為你收集整理的【错误记录】Flutter 界面报错 ( No MediaQuery widget ancestor found. | Scaffold widgets require a MediaQuery )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Flutter】Animation 动
- 下一篇: 【错误记录】Flutter 界面跳转报错