Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery

后端 未结 14 1039
借酒劲吻你
借酒劲吻你 2020-11-28 14:05

I have been trying to get the size of the whole context view in Flutter. But every time I try I\'m getting the above mentioned error. Here\'s my code:

impor         


        
相关标签:
14条回答
  • 2020-11-28 14:52

    MediaQuery is used by Scaffold internal components to layout its children as evident from its source code. Thus, it needs to be wrapped inside a widget which will provide a MediaQuery, like a MaterialApp widget, which inherits from WidgetsApp.

    To fix your MediaQuery.of(context) should be inside the Material Widget. Material app -> scaffold -> MediaQuery.of(context)

    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Loading Page',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyAppPage(),
        );
      }
    }
    

    More Info:MediaQuery.of() called with a context that does not contain a MediaQuery

    0 讨论(0)
  • 2020-11-28 14:54

    In other words, your MediaQuery.of(context) should be inside the Material Widget. Material app -> scaffold -> MediaQuery.of(context)

    0 讨论(0)
提交回复
热议问题