Hello Guys how are you all ? Hope you all are fine. When I run my flutter project Suddenly I got Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery In Flutter in my stacktrack.
Everything was working fine, But as I use MediaQuery.of(context).size; in my StatelessWidget Or StateFulWidget I am Facing This Above Error.
How Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery Error Occurs?
When I Try to use MediaQuery.of(context).size; In My StateFul Or StateLess Widget I am Faced Below Error In My Stack Track.
Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery
Here is my Code.
import 'package:flutter/material.dart';
void main => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return new MaterialApp(
home: new Scaffold(
// Rest of my code
),
);
}
}
How to solve Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery error?
- 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 error To use MediaQuery Inside your Widget makes sure you have used MaterialApp OR WidgetApp Around Your Widget. Cause only MaterialApp Or WidgetApp Provide the MediaQuery Property. When you call .of(context) flutter will always look up the widget tree to find the widget.
- How to solve Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery error?
to solve Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery error To use MediaQuery Inside your Widget makes sure you have used MaterialApp OR WidgetApp Around Your Widget. Cause only MaterialApp Or WidgetApp Provide the MediaQuery Property. When you call .of(context) flutter will always look up the widget tree to find the widget.
Solution 1 : Use MaterialApp
To use MediaQuery Inside your Widget make sure you have used MaterialApp OR WidgetApp Around Your Widget.
Cause only MaterialApp Or WidgetApp Provide the MediaQuery Property. When you call .of(context) flutter will always look up the widget tree to find the widget.
Example Code
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App Title',
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Container(
// Rest of your Code
);
}
}
Solution 2 : use WidgetApp
WidgetApp Also Provide a MediaQuery instead of MaterialApp. We can Also Use Widget App Here. WidgetApp Provides the MediaQuery Property. When you call .of(context) flutter will always look up the widget tree to find the widget.
Hope This Above 5 Solution Will Work For You Too. So it’s all About this error. Hope this tutorial helped you a lot. Comment below Your thoughts and your queries. And Also Comment on your suggestion here.
Also Read
- How to make a blur Background Image effect in Flutter using BackdropFilter.
- Create Rounded Corners Image in Flutter.
- set Background Image to Scaffold in Flutter.
- How To Select multiple images with Flutter
- How to Set Network Image In Circular Avatar In Flutter?
Leave a Reply