Flutter Error MediaQuery of called with a context that does not contain a MediaQuery

0 votes

Hi Guys,

I have been trying to get the size of the whole context view in Flutter. But every time I try I'm getting the below-mentioned error. 

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

How can I solve this error?

Aug 27, 2020 in Others by akhtar
• 38,170 points
2,899 views

1 answer to this question.

0 votes

Hi@akhtar,

You need a MaterialApp or a WidgetsApp around your widget. They provide the media query. When you call .of(context) flutter will always look up the widget tree to find the widget. You can see the below example.

void main() => runApp(App());
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Title',
      theme: kThemeData,
      home: HomePage(),
    );
  }
}
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Container(
      child: ...,
    );
  }
}
answered Aug 27, 2020 by MD
• 95,160 points

Related Questions In Others

0 votes
1 answer

Create a container widget that consumes total space in Flutter.

Hi@akhtar, Instead of giving width and height value, ...READ MORE

answered Jul 28, 2020 in Others by MD
• 95,160 points
208 views
0 votes
1 answer

Error cannot provide both a color and a decoration in Flutter container.

Hi@akhtar, The color and decoration arguments cannot both be supplied, since it ...READ MORE

answered Jul 29, 2020 in Others by MD
• 95,160 points
745 views
0 votes
1 answer

How to retrieve the value of a text field in Flutter App?

Hi@akhtar, In your TextField, you can call one ...READ MORE

answered Sep 3, 2020 in Others by MD
• 95,160 points
443 views
0 votes
1 answer

What is the future of flutter?

Hi@MD, Flutter is a rather new cross-platform framework ...READ MORE

answered Jul 17, 2020 in Others by akhtar
• 38,170 points
159 views
0 votes
1 answer

What is Flutter?

Hi@akhtar, Flutter is an app SDK for building ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,160 points
251 views
0 votes
1 answer

How to install Flutter in Windows system?

Hi@akhtar, You can follow the below-given steps to ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,160 points
429 views
0 votes
1 answer

Row's Children Must Not Contain Any Null Values Flutter Error.

Hi@akhtar, You get this error because in your ...READ MORE

answered Sep 15, 2020 in Others by MD
• 95,160 points
1,699 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,160 points
542 views