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

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

    I fixed it by using the following method. First I created a new class named MyWidget and returned it in MyApp within a MaterialApp's home:. Refer code below:

    import 'package:flutter/material.dart';
    
    void main => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
    
        return new MaterialApp(
          home: new MyWidget(),
        );
      }
    } 
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
    
        final size = MediaQuery.of(context).size;
        return new MaterialApp(
          home: new Scaffold(),
        );
      }
    } 
    

    Also, declaring size as final doesn't matter. Orientation/Rotation is handled.

提交回复
热议问题