1

I am using firestore in flutter web. Everything is working fine in local i.e,Android studio chrome. But when I hosted in firebase hosting (firebase serve) it's not working. The browser console giving error.

Uncaught TypeError: firebase.firestore is not a function
    at Object.arO (top_level.dart:125)
    at cloud_firestore_web.dart:26
    at alN.a (async_patch.dart:308)
    at alN.$2 (async_patch.dart:333)
    at Object.N (async_patch.dart:238)
    at Object.T8 (main.dart:8)
    at js_helper.dart:3221
    at js_helper.dart:3221
    at dartProgram (js_helper.dart:3221)
    at js_helper.dart:3221

pubsec.yaml

name: chitragupta
description: Monthly expenditure tacker

version: 1.2.0+2

environment:
  sdk: ">=2.2.2 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  flutter_launcher_icons: ^0.7.2
  material_design_icons_flutter: ^4.0.5145
  firebase_core: ^0.4.3+2 #For firebase database
  firebase_auth: ^0.16.0 #firebase Authentication
  shared_preferences: ^0.5.7 #for local storage
  shared_preferences_web: ^0.1.2+4
  intl: ^0.16.0
  flutter_datetime_picker: ^1.2.6
  fluttertoast: ^3.1.3
  package_info: ^0.4.0+13
  image_picker: ^0.6.2+3
  http: ^0.12.0+2
  charts_flutter: ^0.8.1
  flutter_svg: ^0.17.4
  cloud_firestore: ^0.13.5


dev_dependencies:
  flutter_test:
    sdk: flutter


flutter:

  uses-material-design: true
  assets:
    - assets/

flutter_icons:
  image_path: "assets/logo.png"
  android: true
  ios: true

I don't know where that line of code is written in my flutter project. Any help to solve this?

Update: App working perfectly fine in Godaddy hosting but not in firebase hosting

BLB
  • 663
  • 1
  • 14
  • 27
  • Hi @BLB you will need to add some code from the files mentioned in the error, so it's possible to better understand your case. Could you please, edit your question and add codes from your files `top_level.dart` and `loud_firestore_web.dart`? This should help have a better understanding of the cause for the error. – gso_gabriel May 04 '20 at 12:45
  • Those are generated files. They don't even located in my project. May be they are from cloud_firestore plugin. – BLB May 04 '20 at 12:53
  • Make sure which ever firestore pub package you are using works with flutter web. Do share your `pubspec.yaml` or at least the dependencies. – Abhilash Chandran May 05 '20 at 10:20
  • Problem is with firebase hosting only. I deployed in Godaddy hosting and it's working fine. Will update the question. – BLB May 05 '20 at 14:52
  • Hi @BLB in the official repository, the `top_level.dart` file, for example, is a file that should be created and configured in your library, for the usage of Dart in Firebase - you can access it [here](https://github.com/FirebaseExtended/firebase-dart/blob/master/lib/src/top_level.dart). Could you please inform if you are doing the connection to Firestore as indicated in the master of this repository? – gso_gabriel May 11 '20 at 10:00
  • By the way if I remember correctly `firebase serve` is only for local testing as per [docs](https://firebase.google.com/docs/hosting/deploying). `firebase deploy` should be the correct command to host it in firebase. Looking at the error you posted the error appears to be from the package `cloud_firestore`. Hope you did follow the setup instructions from this [page](https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore_web/README.md) as per docs. Do check your network tabs if there is CORS error in fetching js files of firestore in index.html. – Abhilash Chandran May 11 '20 at 10:46

1 Answers1

2

I have had similar issues. I fixed it by making sure I referenced all of the correct scripts in my index.html file. Look at the available libraries under Step 5 here: https://firebase.google.com/docs/web/setup#available-libraries

The script tags that are included here do not include defer in the script tag and you may need that to fix your issue eg. <script defer src=...>.

panda
  • 21
  • 1