简体   繁体   中英

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.

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.

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. 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.

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. If so, you need to replace MaterialApp with CupertinoApp . If not, you MUST NOT delete MaterialApp because it is necessary to be there.

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

I wish it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM