简体   繁体   English

在使用不包含 MediaQuery 的上下文调用的 flutter MediaQuery.of() 中出现错误

[英]Getting Error in flutter MediaQuery.of() called with a context that does not contain a MediaQuery

I didn't understand what the error is.我不明白错误是什么。 I am returning a Scaffold widget.我正在返回一个 Scaffold 小部件。 There are no syntax errors detected by visual studio code. Visual Studio 代码未检测到语法错误。 I am getting an error while running the app.运行应用程序时出现错误。


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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
   appBar: AppBar(title: Text("here is my text")),
   body: Text(
     'No, we need bold strokes. We need this plan.',
     style: TextStyle(
         fontWeight: FontWeight.bold,
         color: Colors.blue.withOpacity(0.7),
         height: 1,
         fontSize: 20,
         decoration: TextDecoration.underline,
         decorationColor: Colors.green,
         decorationStyle: TextDecorationStyle.wavy,
         backgroundColor: Colors.red),
   ),
 );
}
}

If I wrap the scaffold widget in the material app it is working.如果我将脚手架小部件包装在材料应用程序中,它就可以工作。 Can anyone explain to me the reason?谁能给我解释一下原因?


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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("here is my text")),
        body: Text(
          'No, we need bold strokes. We need this plan.',
          style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.blue.withOpacity(0.7),
              height: 1,
              fontSize: 20,
              decoration: TextDecoration.underline,
              decorationColor: Colors.green,
              decorationStyle: TextDecorationStyle.wavy,
              backgroundColor: Colors.red),
        ),
      ),
    );
  }
}

The top most widget must be wrapped in a MaterialApp widget.最顶部的小部件必须包装在 MaterialApp 小部件中。 This is what generates the context chain that it's looking for.这就是生成它正在寻找的上下文链的原因。

Media Query is a property of MaterialApp so if you don't use the MaterialApp, you can't access media query. Media Query 是 MaterialApp 的一个属性,所以如果您不使用 MaterialApp,您将无法访问媒体查询。 The proper way is to use it in your main class.正确的方法是在你的主 class 中使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error : MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - MediaQuery.of() called with a context that does not contain MediaQuery MediaQuery.of()使用不包含MediaQuery的上下文调用 - MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error 使用上下文调用的 Mediaquery.of 不包含 mediaquery 错误 - Mediaquery.of called with a context does not contain a mediaquery error (使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。)错误 - (MediaQuery.of() called with a context that does not contain a MediaQuery.) error 在颤动中进行测试会给出错误MediaQuery.of(),该错误使用不包含MediaQuery的上下文调用 - Testing in flutter gives error MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 (紧急援助) - MediaQuery.of() called with a context that does not contain a MediaQuery. (emergency aid) 使用不包含 MediaQuery 的上下文(来自 MaterialApp)调用 MediaQuery.of() - MediaQuery.of() called with a context (from MaterialApp) that does not contain a MediaQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM