No MediaQuery widget ancestor found.The relevant error-causing widget was FutureBuilder<FirebaseApp>

Issue

No MediaQuery widget ancestor found.
The relevant error-causing widget was
FutureBuilder
Scaffold widgets require a MediaQuery widget anchester.The Specific widget that could not find a Mediaquery anchester was:
Scaffold

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:todolist/colorshades/color.dart';
import 'package:todolist/database/services.dart';

import 'Loading.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: Firebase.initializeApp(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return Scaffold(
            body: Center(child: Text(snapshot.error.toString())),
          );
        }
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Loading();
        }
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: TodoList(),
          theme: ThemeData(
            primarySwatch: Colors.pink,
          ),
        );
      },
    );
  }
}

enter image description here

Solution

This error indicates that MediaQuery was referenced before it was provided to the widget tree (which happens in MaterialApp widget in your case). You might want to move your MaterialApp widget to the top of your widget tree, above Scaffold widget (preferably to the very top, above your FutureBuilder).

So it would looke like this (assuming your TodoList and Loading widgets both contain Scaffold, otherwise wrap your FutureBuilder in Scaffold):

return MaterialApp(
  debugShowCheckedModeBanner: false,
  theme: ThemeData(
    primarySwatch: Colors.pink,
  ),
  home: FutureBuilder(
    future: Firebase.initializeApp(),
    builder: (context, snapshot) {
      if (snapshot.hasError) {
        return Scaffold(
          body: Center(child: Text(snapshot.error.toString())),
        );
      }
      if (snapshot.connectionState == ConnectionState.waiting) {
        return Loading();
      }
      return TodoList();
    },
  ),
);

Answered By – FirentisTFW

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *