0

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(),
                        ),
                      ),
                    },
sceee
  • 1,681
  • 19
  • 34
name__
  • 49
  • 1
  • 1
  • 12
  • 1
    Does this answer your question? [No Firebase App '\[DEFAULT\]' has been created - call Firebase.initializeApp() in Flutter and Firebase](https://stackoverflow.com/questions/63492211/no-firebase-app-default-has-been-created-call-firebase-initializeapp-in) – enzo May 07 '21 at 06:08

1 Answers1

5

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());
}
APP
  • 445
  • 1
  • 3
  • 16
user14624595
  • 874
  • 7
  • 19
  • where do await Firebase.initializeApp(); as the main file is some place else ` import 'package:flutter/material.dart'; import 'home.dart'; void main() { runApp(MyApp()); } 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(), ); } } ` – name__ May 07 '21 at 06:11
  • you could write it right after void main, but since it is a future you will also have to await it, so make main async `void main() async{ await Firebase.initializeApp(); runApp(MyApp());}` – user14624595 May 07 '21 at 06:14
  • i just updated the code could you please chech it once – name__ May 07 '21 at 06:15
  • I don't see you calling `await Firebase.initializeApp(); `anywhere – user14624595 May 07 '21 at 06:16
  • Do it ABOVE the runApp(); `void main() async { await Firebase.initializeApp(); runApp(MyApp()); }` – user14624595 May 07 '21 at 06:22
  • now it is working thank you – name__ May 07 '21 at 06:23
  • also now there is a new error , i understand if you don`t wanna help! Class 'DocumentSnapshot' has no instance getter 'documents'. Receiver: Instance of 'DocumentSnapshot' Tried calling: documents – name__ May 07 '21 at 06:31
  • Perhaps you should post another question, plus this is getting pretty long, I think there is a chat functionality in stackoverflow, but I am not sure if it is available to new users. – user14624595 May 07 '21 at 06:33
  • the chat option isn`t working either... so I guess it is not available for me yet – name__ May 07 '21 at 06:35