简体   繁体   English

MaterialApp 和“使用不包含 MediaQuery 的上下文调用的 MediaQuery.of()”

[英]MaterialApp and "MediaQuery.of() called with a context that does not contain a MediaQuery"

I am recieving the following error When i attempt to use MediaQuery in my App:当我尝试在我的应用程序中使用 MediaQuery 时,我收到以下错误:

"MediaQuery.of() called with a context that does not contain a MediaQuery" “使用不包含 MediaQuery 的上下文调用 MediaQuery.of()”

I have done some research and everything i read says that if i use either MaterialApp or WidgetsApp i should have access to MediaQuery.我做了一些研究,我读到的所有内容都说,如果我使用 MaterialApp 或 WidgetsApp,我应该可以访问 MediaQuery。

However I am using MaterialApp as a parent widget from the location i am using MediaQuery... so i dont understand why i dont have access to the MediaQuery in the context that i am using...??但是,我在使用 MediaQuery 的位置使用 MaterialApp 作为父小部件...所以我不明白为什么我在使用的上下文中无法访问 MediaQuery...??

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  List<Map<String, String>> elementList;
  String elementName;

  MyApp() {
    this.elementName = "TestName";
    this.elementList = List<Map<String, String>>();
    this.elementList.add({"element1": "Value1"});
    this.elementList.add({"element2": "Value2"});
    this.elementList.add({"element3": "Value3"});
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Column(
        children: <Widget>[

          Container(
            height: MediaQuery.of(context).size.height * 0.15,
            child: UNavbar(),
            ),

          Container(
            height: MediaQuery.of(context).size.height * 0.7,
            child: ListView.builder(              
              itemBuilder: (ctx, index) {
               return UNode(elementName, elementList);              
              }              
            )
          ),          
          Container(
            height: MediaQuery.of(context).size.height * 0.15,
            alignment: Alignment.bottomCenter,
            child: UTextbox(),
          ),

        ],
      ),
    );
  }
}```

I think MediaQuery require any parent widget which have Mediadata as Materialapp have, so if you create a separate widget which contain MaterialApp widget and then put all other code in separate widget.我认为 MediaQuery 需要任何具有 Mediadata 的父小部件,因为 Materialapp 具有,所以如果您创建一个单独的小部件,其中包含 MaterialApp 小部件,然后将所有其他代码放在单独的小部件中。 it will work.它会起作用。

Note : I replaced all your widget with text widget and i wrap column with scaffold other wise you will get black screen.注意:我用文本小部件替换了你所有的小部件,我用脚手架包裹了列,否则你会得到黑屏。

Following code help you more about it.以下代码可以帮助您更多地了解它。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  List<Map<String, String>> elementList;
  String elementName;

  MyApp() {
    this.elementName = "TestName";
    this.elementList = List<Map<String, String>>();
    this.elementList.add({"element1": "Value1"});
    this.elementList.add({"element2": "Value2"});
    this.elementList.add({"element3": "Value3"});
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Home());
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
     body: Column(
      children: <Widget>[
        Container(
          height: MediaQuery.of(context).size.height * 0.15,
          child: Text(""),
        ),
        Container(
            height: MediaQuery.of(context).size.height * 0.7,
            child: ListView.builder(itemBuilder: (ctx, index) {
              return Text("");
            })),
        Container(
          height: MediaQuery.of(context).size.height * 0.15,
          alignment: Alignment.bottomCenter,
          child: Text("data"),
        ),
      ],
    ));
  }
}

The issue is happening because the context you are using in the MediaQuery won't have a MaterialApp associated with that.发生此问题是因为您在 MediaQuery 中使用的上下文没有与之关联的 MaterialApp。 It's passed as an argument of the build method and you are creating the material app inside the same build method.它作为 build 方法的参数传递,您正在同一个 build 方法中创建材料应用程序。 To fix this you can use the Builder widget.要解决此问题,您可以使用Builder小部件。

home: Builder(builder: (context) {
  return Column(
     children: <Widget>[
       Container(
         height: MediaQuery.of(context).size.height * 0.15,
         child: UNavbar(),
         ),
       // Remaining Children
     ],
  );
),

暂无
暂无

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

相关问题 使用不包含 MediaQuery 的上下文(来自 MaterialApp)调用 MediaQuery.of() - MediaQuery.of() called with a context (from MaterialApp) 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 即使在MaterialApp小部件下包装了MediaQuery.of()时,该上下文也不包含MediaQuery - MediaQuery.of() called with a context that does not contain a MediaQuery even when Wrapped under the MaterialApp Widget 使用不包含 MediaQuery 的上下文调用 MediaQuery.of(),即使在 MaterialApp 中也是如此 - MediaQuery.of() called with a context that does not contain a MediaQuery even when inside MaterialApp 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 a MediaQuery. (emergency aid) 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error 在使用不包含 MediaQuery 的上下文调用的 flutter MediaQuery.of() 中出现错误 - Getting Error in flutter MediaQuery.of() called with a context that does not contain a MediaQuery 使用上下文调用的 Mediaquery.of 不包含 mediaquery 错误 - Mediaquery.of called with a context does not contain a mediaquery error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM