Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery
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(),
);
}
}
