'Error "[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()" is thrown [duplicate]

I have a main file which returns home page and on the home page I am trying to call a new file (test.dart). Now the problem is this test.dart file is throwing some errors which I am unable to solve as I am completely new to flutter and Firebase Firestore. Here is the code for test.dart:

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
// import 'package:vola1/colors.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';

class test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        body: StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('countries')
          .doc('nW9L4LGpn2MZVyiTyUII')
          .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text('Loading data.. please wait..');
        return Container();
      },
    ));
  }
}


This is the error it is throwing

======== Exception caught by widgets library =======================================================
The following FirebaseException was thrown building test(dirty):
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

The relevant error-causing widget was: 
  test file:///D:/flutter%20course/vola1/lib/home.dart:88:49
When the exception was thrown, this was the stack: 
#0      MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:122:5)
#1      Firebase.app (package:firebase_core/src/firebase.dart:54:41)
#2      FirebaseFirestore.instance (package:cloud_firestore/src/firestore.dart:40:21)
#3      test.build (package:vola1/test.dart:15:33)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4569:28)
...
====================================================================================================

main file

import 'package:flutter/material.dart';
import 'home.dart';

void main() async {
  runApp(MyApp());
  await Firebase.initializeApp();
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

home page where the button is calling test.dart

ElevatedButton(
                    onPressed: () => {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => test(),
                        ),
                      ),
                    },


Solution 1:[1]

Before using Firebase, you have to call Firebase.initializeApp(); You could do it like this:

 void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 APP