DEV Community

Hari Pd. Chaudhary
Hari Pd. Chaudhary

Posted on

[Solved] No MediaQuery widget found Error in Flutter

You may got this error in Flutter if you haven't placed MaterialApp() widget correctly in widget tree. See the solution below to solve this error:

Error Message:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════
No MediaQuery widget ancestor found.
MyApp widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was:
  MyApp
The ownership chain for the affected widget is: "MyApp ← [root]"
Enter fullscreen mode Exit fullscreen mode

Solution 1:

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp( //use MaterialApp() widget like this
      home: Home() //create new widget class for this 'home' to 
                   // escape 'No MediaQuery widget found' error
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Solution 2:

MediaQuery(
      data: MediaQueryData(),
      child: MaterialApp()
)
Enter fullscreen mode Exit fullscreen mode

This answer is referenced from: How to Solve ’No MediaQuery widget found’ Error in Flutter

Top comments (0)