日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【错误记录】Flutter 使用 MediaQuery 适配全面屏报错 ( No MediaQuery widget ancestor found. )

發布時間:2025/6/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【错误记录】Flutter 使用 MediaQuery 适配全面屏报错 ( No MediaQuery widget ancestor found. ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、報錯信息
  • 二、解決方案





一、報錯信息



需要使用 MediaQuery 獲取當前的 Padding ;

import 'package:flutter/material.dart';/// 使用 MediaQuery 進行全面屏適配 void main() {runApp(MyApp()); }class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {/// 獲取當前的 padding 信息 , 報錯位置 ; final EdgeInsets edgeInsets = MediaQuery.of(context).padding;return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: Container(decoration: BoxDecoration(color: Colors.white,),//padding: EdgeInsets.fromLTRB(0, edgeInsets.top, 0, edgeInsets.bottom),child: SafeArea(child: Column(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [Text("頂部內容"),Text("底部內容"),],),),),);} } Performing hot reload... Syncing files to device TAS AN00...======== Exception caught by widgets library ======================================================= The following assertion was thrown building MyApp(dirty): No MediaQuery widget ancestor found.MyApp widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: MyAppdirty The ownership chain for the affected widget is: "MyApp ← [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: MyApp file:///D:/002_Project/002_Android_Learn/flutter_screen_adaption/lib/main.dart:4: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 MyApp.build (package:flutter_screen_adaption/main.dart:14:46) #4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28) ... ==================================================================================================== Reloaded 1 of 553 libraries in 299ms.





二、解決方案



獲取 Padding 信息 ,

/// 獲取當前的 padding 信息 , 報錯位置 ; final EdgeInsets edgeInsets = MediaQuery.of(context).padding;

需要在 MaterialApp 中進行獲取 , 這里將上述代碼重新進行封裝 ,

將 MediaQuery 調用的代碼 , 封裝到 MaterialApp 組件內部 ;

import 'package:flutter/material.dart';/// 使用 MediaQuery 進行全面屏適配 void main() {runApp(MyApp()); }class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: HomePage(),);} }class HomePage extends StatelessWidget{@overrideWidget build(BuildContext context) {/// 獲取當前的 padding 信息final EdgeInsets edgeInsets = MediaQuery.of(context).padding;return Container(decoration: BoxDecoration(color: Colors.white,),padding: EdgeInsets.fromLTRB(0, edgeInsets.top, 0, edgeInsets.bottom),child: Column(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [Text("頂部內容"),Text("底部內容"),],),);} }

此時報錯信息處理完畢 ;

總結

以上是生活随笔為你收集整理的【错误记录】Flutter 使用 MediaQuery 适配全面屏报错 ( No MediaQuery widget ancestor found. )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。