Home > front end >  Why am i getting No MediaQuery widget ancestor found .error
Why am i getting No MediaQuery widget ancestor found .error

Time:06-17

I have checked some threads here on StackOverlow but they dont fix my problem.The suggestions are

  1. Create a new Stateless/Stateful widget and pass it to the home parameter OR
  2. Use the Builder widget and pass it to the home parameter.

which I already did.

This is my main.dart file

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context,designSize: Size(360,640));
    return MaterialApp(
      title: 'Flutter Demo',

      home: HomeScreen()
    );
  }
}

And this is home.dart file

 class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: Row(
            children: [
              buildLeftColumn(),
              SizedBox(
                width: 20.w,
              ),
              //    buildRightColumn(),
            ],
          )),
    );
  }

   buildLeftColumn() {
    return Container();
  }

So. what am i doing wrong.Could you please help

CodePudding user response:

If you like to use ScreenUtil.init(...) you can solve this issue calling it on HomeScreen widget(context).

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, designSize: Size(360, 640));

To use it before MaterialApp you can use ScreenUtilInit widget.

Widget build(BuildContext context) {
  return ScreenUtilInit(
    designSize: Size(360, 640),
    builder: (context, child) => MaterialApp(
      title: 'Flutter Demo',
      home: HomeScreen(),
    ),
  );
}
  • Related