flutter error: mediaquery.of() called with a context that does not contain a mediaquery

Ripple Error: MediaQuery.of() called with a setting that doesn’t 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 blunder :

MediaQuery.of(context).size.height,

Why I got this problem ?

MediaQuery is utilized by Scaffold inside segments to design its youngsters as clear from its source code. Subsequently, it should be wrapped inside a gadget which will give a MediaQuery, similar to a MaterialApp gadget, which acquires from WidgetsApp.

How to fix?

As such, your MediaQuery.of(context) should be inside the Material Widget. Material application – > framework – > 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(),
    );
  }
}

Leave a Comment