Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter web Firebase initialization error

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?

like image 535
blob Avatar asked Oct 27 '25 08:10

blob


1 Answers

 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)

like image 171
Pooja Avatar answered Oct 28 '25 23:10

Pooja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!