4

When I start the flutter web app I get this error:

[core/not-initialized] Firebase has not been correctly initialized. Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp.

<!DOCTYPE html>
<html>
<head>
    ...
</head>
<body>
  <script type="module">
    // Import the functions you need from the SDKs you need
    import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.0/firebase-app.js";
    import { auth } from 'https://www.gstatic.com/firebasejs/9.9.0/firebase-auth.js'
    import { firestore } from 'https://www.gstatic.com/firebasejs/9.9.0/firebase-firestore.js'  
    // https://firebase.google.com/docs/web/setup#available-libraries
  
    // Your web app's Firebase configuration
    const firebaseConfig = {
      apiKey: "xxx",
      authDomain: "xxx",
      projectId: "xxx",
      storageBucket: "xxx",
      messagingSenderId: "xxx",
      appId: "xxx"
    };
  
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
  </script>  
  <!-- This script installs service_worker.js to provide PWA functionality to
       application. For more information, see:
       https://developers.google.com/web/fundamentals/primers/service-workers -->
  <script>
         ...
  </script>
</body>
</html>

As you can see firebase is initialized in the index.html file. I have installed all the needed libraries, this is the list:

  • firebase_auth: ^3.4.2
  • cloud_firestore: ^3.3.0
  • firebase_core: ^1.19.2

Should I init firebase in flutter?

1

1 Answer 1

3
 if (kIsWeb) 
    await Firebase.initializeApp(
      options: FirebaseOptions(
        apiKey: "xxx",
        appId: "xxx",
        messagingSenderId: "xxx",
        projectId: "xxx",
      ),
    );
else
   await Firebase.initializeApp();

Add the above code in your main.dart file. (kIsWeb will check whether the application is running on the web)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.