No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase

211,413

Solution 1

If you followed Peter's answer and are still getting the same error, check to make sure anything else you have in your main function comes after the await Firebase.initializeApp() call, like so:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
  FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
  runApp(MyApp());
}

Solution 2

Since August 2017, all Firebase services have been updated so that you have to call Firebase.initializeApp() in your main before you can use any of the Firebase products, for example:

If you want to use the firebase_core plugin in a flutter application, then in your pubspec.yaml file add the firebase_core as below

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^1.19.1

Then call the Firebase.initializeApp(): in your app. same as something below:

import 'package:firebase_core/firebase_core.dart';

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

Solution 3

First, add this dependency:

firebase_core :

Second: in the project main function, add these two lines and make the function async:

void main() async {
    // These two lines
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();

    //
    runApp(MyApp());
}

Now you can use Firebase normally in any file or widget in the project.

The FutureBuilder widget will work either, but you must add it every time you want to access Firebase.

Solution 4

Peter's answer is perfect!! But if you still get an error in your code and following the Flutter Firebase codelab, note that those tutorials are outdated as of August 2020 and not updated yet.

You need to do many other changes like:

  • replace .data with .data()
  • replace updateData with update

Solution 5

Flutter Web with Firebase

If you are using Flutter Web with Firebase make sure you have the SDKs installed in the body tag of your ./web/index.html

  <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-analytics.js"></script>

Furthermore, make sure you call firebase.initializeApp(...) with the configuration parameters in the index.html as well.

  <script>
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    var firebaseConfig = {
      apiKey: "AIz...",
      authDomain: "...",
      databaseURL: "https://<project-name>.firebaseio.com",
      projectId: "...",
      storageBucket: "<project-name>.appspot.com",
      messagingSenderId: "...",
      appId: "...",
      measurementId: "..."
    };
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
  </script>

At last configure firebase as described in Peter Haddad's answer:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
Share:
211,413
Kennedy Owusu
Author by

Kennedy Owusu

Tech Fanatic | Mobile App Development Enthusiast | Web Development Zealot

Updated on July 23, 2022

Comments

  • Kennedy Owusu
    Kennedy Owusu almost 2 years

    I am building a Flutter application and I have integrated Firebase, but I keep getting this error when I click on a button either to register, login or logout. I have seen other people have asked the same question, but none seems to work for me. I am using Flutter and Android Studio. How can I fix this problem?

    This is an excerpt of my code

    class HomeScreen extends StatefulWidget {
      @override
      _HomeScreenState createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.red,
          body: Center(
            child: Container(
              child: RaisedButton(
                onPressed: () {
                  FirebaseAuth.instance.signOut().then((value) {
                    Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>
                                LoginScreen()));
                  });
                },
                child: Text("Logout"),
              )
            )
          )
        );
      }
    }
    

    Below is the thrown exception

    ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
    The following FirebaseException was thrown while handling a gesture:
    [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
    
    When the exception was thrown, this was the stack:
    
    #0      MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:118:5)
    #1      Firebase.app (package:firebase_core/src/firebase.dart:52:41)
    #2      FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:37:47)
    #3      _HomeScreenState.build.<anonymous closure> (package:cosytok/screens/home.dart:20:28)
    #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
    #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
    #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
    #7      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
    #8      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
    #9      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
    #10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
    #11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
    #12     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
    #13     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
    #14     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
    #15     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
    #16     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
    #17     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
    #18     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
    #19     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
    #20     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
    #24     _invoke1 (dart:ui/hooks.dart:267:10)
    #25     _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
    (elided 3 frames from dart:async)
    
    Handler: "onTap"
    Recognizer:
      TapGestureRecognizer#f0104
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    
    ════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
    The following FirebaseException was thrown while handling a gesture:
    [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
    
  • Roman Soviak
    Roman Soviak about 3 years
    That is not securely put sensitive data inside .html
  • Jens
    Jens about 3 years
    @user7856586 what would you say is the sensitive data in this case?
  • Roman Soviak
    Roman Soviak about 3 years
    sorry I mean that we should also fix rules on Gcloud, cause storing naked data in html file is not secure. Look here
  • Peter Mortensen
    Peter Mortensen about 3 years
    An explanation would in order. E.g., what is the idea/gist? Please respond by editing your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).