artistAhmed
artistAhmed

Reputation: 327

No MediaQuery widget Ancestor found on Main.dart Screen

I am trying to make two buttons that will navigate users to two different screens, I don't know what I am doing wrong but I am getting an error on the runtime.

I am getting the following error on my app screen when I run the app which says.

No MediaQuery widget ancestor found.
MyApp widgets require a MediaQuery widget ancestor. 
....etc etc

I have added these two lines inside my code but they don't work.

 width: MediaQuery.of(context).size.width,
 height: MediaQuery.of(context).size.height,

This is my Main.dart file

import 'package:flutter/material.dart';
import 'package:relationship/screens/form_screen.dart';
import 'package:relationship/screens/personal_log.dart';

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

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // ignore: deprecated_member_use
            RaisedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => PersonalOg()));
              },
              child: Text("Personal Log"),
            ),
            // ignore: deprecated_member_use
            RaisedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => FormClass()));
              },
              child: Text("Lover Log"),
            )
          ],
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 190

Answers (1)

Viraj Doshi
Viraj Doshi

Reputation: 881

Wrap your Scaffold inside a MaterialApp widget

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

class MyApp extends StatelessWidget {
  @override
  Widget build (BuildContext context) {
    return MaterialApp(
      home: YourApp();
    );
  }
}

Replace YourApp in this code with the Scaffold in MyApp in your code.

A flutter app requires a MaterialApp as a parent widget (even CupertinoApp would do but CupertinoApp has/had some problems when I used it some time back.) before any other widget.

Upvotes: 1

Related Questions