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

VNBNews.vn
Nov 7 · 1 min read

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

My code got problem :

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

This line got error :

MediaQuery.of(context).size.height,

Why I got this problem ?

MediaQuery is used by Scaffold internal components to layout its children as evident from its source code. Thus, it needs to be wrapped inside a widget which will provide a MediaQuery, like a MaterialApp widget, which inherits from WidgetsApp.

How to fix?

In other words, your MediaQuery.of(context) should be inside the Material Widget. Material app -> scaffold -> MediaQuery.of(context)

class Loading extends StatelessWidget {
// This widget is the root of your application.

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Loading Page',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoadingPage(),
);
}
}

CodeSpace69

Talk about bugs.

VNBNews.vn

Written by

Vnbnews.vn

CodeSpace69

Talk about bugs.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade