Home > database >  FlutterError (No MediaQuery widget ancestor found. I had used the MaterialApp, but still got error
FlutterError (No MediaQuery widget ancestor found. I had used the MaterialApp, but still got error

Time:07-30

Where has problems in my main.dart? I had used the MaterialApp, but still got error FlutterError (No MediaQuery widget ancestor found.

Do I miss something? But if I remove the MaterialApp, still got error

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/images/my_bg.png'),
                fit: BoxFit.cover),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Container(
                padding: const EdgeInsets.all(16),
                width: MediaQuery.of(context).size.width,
                child: AuthGate(),
              )
            ],
          ),
        ),
      ),
    );
  }

CodePudding user response:

Try separating materialApp from app widget context.

void main() async {
  runApp(
    MaterialApp(
      home: MyApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage('assets/images/my_bg.png'), fit: BoxFit.cover),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
                padding: const EdgeInsets.all(16),
                width: MediaQuery.of(context).size.width,
                child: Text("AA")
                // AuthGate(),
                )
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

runApp(MaterialApp( home: MyApp(), debugShowCheckedModeBanner: false, ));

  • Related