简体   繁体   English

使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 (紧急援助)

[英]MediaQuery.of() called with a context that does not contain a MediaQuery. (emergency aid)

I deleted the 'MaterialApp' code block because I couldn't write the codes I wanted at the beginning.我删除了“MaterialApp”代码块,因为我一开始无法编写我想要的代码。

Now it gives an error, how can I fix this?现在它给出了一个错误,我该如何解决这个问题? I have to handle this in a very short time, I have to put the codes I wrote in the "MaterialApp" block, but I can't.我必须在很短的时间内处理这个问题,我必须把我写的代码放在“MaterialApp”块中,但我做不到。

Can you help me?你能帮助我吗?

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
    return CupertinoTabScaffold(
        tabBar: CupertinoTabBar(
            items: < BottomNavigationBarItem > [
                new BottomNavigationBarItem(
                    icon: new Icon(Icons.home),
                    title: Text('Enes'),
                ),
                new BottomNavigationBarItem(
                    icon: new Icon(Icons.bluetooth),
                    title: Text('Mehmet'),
                ),
            ],
        ),
        tabBuilder: (BuildContext context, int index) {
            return CupertinoTabView(
                builder: (BuildContext context) {
                    return CupertinoPageScaffold(
                        navigationBar: CupertinoNavigationBar(
                            middle: Text('Page 1 of tab $index'),
                        ),
                        child: Center(
                            child: CupertinoButton(
                                child: const Text('Next Page'),
                                    onPressed: () {
                                        Navigator.of(context).push(
                                            CupertinoPageRoute < void > (
                                                builder: (BuildContext context) {
                                                    return CupertinoPageScaffold(
                                                        navigationBar: CupertinoNavigationBar(
                                                            middle: Text('Page 2 of tab $index'),
                                                        ),
                                                        child: Center(
                                                            child: CupertinoButton(
                                                                child: const Text('Back'),
                                                                    onPressed: () {
                                                                        Navigator.of(context).pop();
                                                                    },
                                                            ),
                                                        ),
                                                    );
                                                },
                                            ),
                                        );
                                    },
                            ),
                        ),
                    );
                },
            );
        },
    );
}

} }

Not sure why it must be in the MaterialApp.不知道为什么它必须在 MaterialApp 中。 This solution is what I have deployed in the past:这个解决方案是我过去部署的:

In your MaterialApp, SetupStuff (could be named anything) is set as your home or initialRoute.在您的 MaterialApp 中,SetupStuff(可以命名任何东西)被设置为您的 home 或 initialRoute。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      color: Colors.yellow[100],
      debugShowCheckedModeBanner: false,
      title: 'MyApp',
      theme: currentTheme,
      home: SetupStuff(),
    );
  }
}

class SetupStuff extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //  This is the first 'context' with a MediaQuery, therefore,
    //  this is the first opportunity to set MediaQuery based values

    //Set values / do things here.

    WidgetsBinding.instance.addPostFrameCallback((_) {
      Navigator.pushReplacement(context,
          MaterialPageRoute(builder: (BuildContext context) => AlsoMyApp()));
    });

    return SafeArea(child: Material(color: Colors.yellow[300]));
  }
}

It seems like you want to develop an app with iOS elements.您似乎想开发一个带有 iOS 元素的应用程序。 If so, you need to replace MaterialApp with CupertinoApp .如果是这样,您需要将MaterialApp替换为CupertinoApp If not, you MUST NOT delete MaterialApp because it is necessary to be there.如果没有,您不得删除MaterialApp ,因为它必须存在。

class CupertinoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
   return CupertinoApp(
     home: CupertinoHomePage(),
    );
  }
}

I wish it helps.我希望它有所帮助。

暂无
暂无

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

相关问题 (使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。)错误 - (MediaQuery.of() called with a context that does not contain a MediaQuery.) error 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error 使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - MediaQuery.of() called with a context that does not contain MediaQuery 如何修复“使用不包含 MediaQuery 的上下文调用 MediaQuery.of()”的错误? - How can I fix the error of “MediaQuery.of() called with a context that does not contain a MediaQuery.”? MediaQuery.of()使用不包含MediaQuery的上下文调用 - MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 如何修复此 dart 代码? - MediaQuery.of() called with a context that does not contain a MediaQuery. How to fix this dart code? Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文(来自 MaterialApp)调用 MediaQuery.of() - MediaQuery.of() called with a context (from MaterialApp) that does not contain a MediaQuery 在使用不包含 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