35

I'm trying to use Firebase in my flutter web project.
But app can't be run with this message.

TypeError: dart.global.firebase.firestore is not a function
at Object.firestore$ [as firestore] 

My index.html looks like this.

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

<script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "MY_API_KEY",
    authDomain: "MY_AUTHDOMAIN",
    databaseURL: "MY_URL",
    projectId: "MY_ID",
    storageBucket: "MY_BUCKET",
    messagingSenderId: "MY_ID",
    appId: "MY_APPID",
    measurementId: "MY_ID"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>

Anyone knows how to fix this?

1
  • Please share your pubspec.yaml. Commented Aug 4, 2020 at 14:08

11 Answers 11

58

As mentioned in the docs, add this line in index.html:

  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>
Sign up to request clarification or add additional context in comments.

8 Comments

really? it did for me.
What does this do and I wonder why is it not mentioned on the official link? Anyone knows? Please explain me
I also did a flutter upgrade along with this an the web app loaded
It sort of is and sort of isn't mentioned in the docs. To initialize you add the script firebase-app.js, but then if you want firestore database you add firebase-firestore.js - then you need to do that same thing for any other product you want eg analytics, messaging, storage, etc. just like you would in vanilla js I believe
Adding to the answer, in my case after updating the above line I had to stop the running project and run it again to make that work.
|
14

I had the same thing, to fix it just add this in the index.html at the bottom:

...
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js"></script>
  <script src="main.dart.js" type="application/javascript"></script>
...

just make sure it's after:

....
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
...

3 Comments

If I put this AFTER firebase.initializeApp() then I get Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp()
this is in the dart code, so go and before calling any firebase method, initialize the firebase app, by calling firebase.initializeApp(), just make a splash screen and add this as a future when it finishes, navigate to the next page.
add the scripts then call the dart.js, in dart code call the firebase app
7

I think what you are missing is the Firestore SDK to solve that Just go to this URL if you will go with CDN

Available Firebase JS SDKs (from the CDN)

then add whatever library you are using for your case it's Firestore so it should be this one:

<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-firestore.js"></script>

add it on the top of your body

Comments

5

In latest version of FlutterFire, I found these issues and unable to resolve. I resolved this issues while downgrading the dependencies of FlutterFire . Since these are in beta version , we will face these issues. Hope in upcoming version of flutterfire ,these issues will resolve. Follow these steps:--

1) Add these dependencies in pubsec.yaml file.

  firebase_core: ^0.5.0
  firebase_auth: ^0.18.0+1
  cloud_firestore: ^0.14.3+1

2) Then add these scripts in index.html file.

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

3) After that Add these scripts before main.dart.js scripts.

 <script>
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  var firebaseConfig = {
    apiKey: "your api key",
    authDomain: "app_name.firebaseapp.com",
    projectId: "app_name2",
    storageBucket: "app_name.appspot.com",
    messagingSenderId: "882321312y",
    appId: "1:1782944473493525:web:923488234",
    measurementId: "G-4dmfnsd"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
  </script>

Note:- This file is a generated file while adding WebApp to existing Flutter Project on Firebase Console.

Comments

5

You have to add ALL the CDN's you need from Firebase. Not only the ones in the example.

If you didn't do it yet, you have to:

  1. Go to your Firebase Console
  2. Create a new Web App for your project if you didn't Project settings Web App (The one you created)
  3. Select CDN and copy and paste it in your index.html inside body, before the <script> tag containing the main.dart.js

If you did it, and it is still not working it is because you need to add the CDN for the other services you are using.

In my case they were firebase-auth.js and firebase-firestore.js. But don't copy it from a reply here because it could be outdated. You can copy the path for your firebase-app.js which includes the current version. Mine is <script src="https://www.gstatic.com/firebasejs/8.4.2/firebase-app.js"> meaning my current version is 8.4.2 that could not be your case.

Comments

2

insert this script at first before you use any other script because this script needs to get call first then other firebase scripts.

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

Comments

1

For anyone else that arrives here, and is just starting with Firebase, you may not have setup your firebase configuration correctly (e.g you're following a tutorial on running Firebase in say, Flutter)

If so, make sure you follow steps like so to add your firebase app configuration info to your HTML file

In short:

  • In the Firebase console, select Project Overview in the left navigation bar, and click the Web button under Get started by adding Firebase to your app.
  • Give your app a nickname. This is the value that is used across the Firestore console to identify the web version of your app.
  • Click Register app.
  • After you register your app, the Add Firebase SDK step gives you some code you need to paste into the web/index.html file of your app. Do so!

Comments

1

check the order/presence of tags ,in the following picture :

index.html

check this for latest versions : Firebase JS SDKs(from CDN)

Comments

0

check your pubspec.yaml, and import all firebase module. I means, if you have firebase_auth in your pubspec.yaml, you should also import script https://www.gstatic.com/firebasejs/VERSION/firebase-auth.js

Comments

0

FYI, here is the list: https://firebase.google.com/docs/hosting/reserved-urls

Every service needs its own CDN import

Comments

0

Just changing values to latest version from 8.6.2 to 8.10.0 solve my error.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.