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

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

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.

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

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,
        ),
      )
    );
  }
}

And I am facing error at this line

MediaQuery.of(context).size.height,

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.

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

  1. How to solve Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery?

    to solve Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery Solution is MediaQuery.of(context) line must be in  Material app -> scaffold -> MediaQuery.of(context). That’s why We have to use MaterialApp -> Scaffold And then We can use MediaQuery.of(context). Below Is My Example Code For Reference.

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

    to solve Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery Solution is MediaQuery.of(context) line must be in  Material app -> scaffold -> MediaQuery.of(context). That’s why We have to use MaterialApp -> Scaffold And then We can use MediaQuery.of(context). Below Is My Example Code For Reference.

Solution 1 : use MaterialApp > Scaffold

Solution is MediaQuery.of(context) line must be in  Material app -> scaffold -> MediaQuery.of(context). That’s why We have to use MaterialApp -> Scaffold And then We can use MediaQuery.of(context). Below Is My Example Code For Reference.

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Title',
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return Container(
      
    );
  }
}

Summery

Now, Error is solved. If you still facing this error so Comment below your error with code. I Will help You. Comment below Your thoughts and your queries. And Also Comment on your suggestion here.

Also Check Out Below Tutorials


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *