DEV Community

Cover image for Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery - fluttercorner.com
FlutterCorner
FlutterCorner

Posted on

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

Hello Guys. Many times we Face Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery. So in this tutorial, we are going to Solve this error.

Suppose This is My Code

return new Container(
      height: MediaQuery.of(context).size.height,
      decoration: BoxDecoration(
        color: backgroundColor,
      ),
      child:   DecoratedBox(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Center(

                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(height: 120.0, color: Colors.yellow, child: new ColorLoader2(color1: Colors.redAccent, color2: Colors.deepPurple, color3: Colors.green),),

                ],
      ),
            )
        ],
      ),
        decoration: BoxDecoration(
          color: Colors.redAccent,
        ),
      )
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

And I am facing error at this line

MediaQuery.of(context).size.height,
What is The problem ?
Reason behind this issue is MediaQuery Is used by Scaffolds internal component. Thats why it must be wrapped inside a widget which will provide us MediaQuery Something like MaterialApp Widget or WidgetsApp . Material App Will Provide Mediaquery and that will solve our error.

Solution
Here is all possible solution added please visit Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery

Top comments (0)