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

后端 未结 14 1037
借酒劲吻你
借酒劲吻你 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:31

    What works for us is using WidgetsBinding.instance.window instead of MediaQuery - also when setting the theme of the MaterialApp:

    _pixelRatio = WidgetsBinding.instance.window.devicePixelRatio;
    _screenWidth = WidgetsBinding.instance.window.physicalSize.width;
    _screenHeight = WidgetsBinding.instance.window.physicalSize.height;
    _statusBarHeight = WidgetsBinding.instance.window.padding.top;
    _bottomBarHeight = WidgetsBinding.instance.window.padding.bottom;
    _textScaleFactor = WidgetsBinding.instance.window.textScaleFactor;
    
    0 讨论(0)
  • 2020-11-28 14:35

    Had the same error in

    import 'screens/tasks_screen.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return TasksScreen();
    
      }
    }
    

    I solved it by:-

    import 'package:flutter/material.dart';
    import 'screens/tasks_screen.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: TasksScreen(),
        );
      }
    }
    
    0 讨论(0)
  • 2020-11-28 14:36

    Wrap your code in a Material App widget. I also had the same issue as I forgot to use it and directly returned the scaffold.

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

    0 讨论(0)
  • 2020-11-28 14:36
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyAppOne(),
        );
      }
    }
    class MyAppOne extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    class _MyAppState extends State<MyAppOne>{
      @override
      Widget build(BuildContext context){
        return Scaffold(
        );
      }
    }
    
    0 讨论(0)
  • 2020-11-28 14:41

    You can access MediaQuery when you are inside MaterialApp. The place where you are accessing the media query is not correct.

    Please refer below code:

    import 'package:flutter/material.dart';
    
    class CommonThings {
      static Size size;
    }
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'MediaQuery Demo',
          theme: new ThemeData(
            primarySwatch: Colors.red,
          ),
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        CommonThings.size = MediaQuery.of(context).size;
        print('Width of the screen: ${CommonThings.size.width}');
        return new Container();
      }
    }
    

    I've purposely created a class CommonThings which has static Size so that you can use it throughout the app.

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

    Solved by re-run the app(click on stop button in android studio then run again)

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