I have been trying to get the size of the whole context view in Flutter. But every time I try I'm getting the above mentioned error. Here's 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(),
);
}
}
Note: I also tried with a StatefulWidget
.
Please, help me find what I'm doing wrong here.
I fixed it by using the following method. First I created a new class named
MyWidget
and returned it inMyApp
within aMaterialApp
'shome:
. Refer code below:Also, declaring size as final doesn't matter. Orientation/Rotation is handled.
Had the same error in
I solved it by:-
Wrap your code in a Material App widget. I also had the same issue as I forgot to use it and directly returned the scaffold.
In other words, your MediaQuery.of(context) should be inside the Material Widget. Material app -> scaffold -> MediaQuery.of(context)