firebaseflutterdartgoogle-cloud-platformgoogle-cloud-firestore

Firebase cloud firestore not working in flutter


I am building this app that makes use of cloud firestore. The problem is that firestore is not working. Here is what code I am using

Db.dart

class Db{

  // collection reference
  static final CollectionReference _collectionReference = Firestore.instance.collection("codes");

  static Future addOrUpdateCode(String code , double lat , double long) async {
    return await _collectionReference.document(code).setData({
      'lat':lat,
      'long':long
    });
  }

}

I have added this line in pubspec.yaml

cloud_firestore: ^0.13.7

I am getting these errors

I/BiChannelGoogleApi(13145): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@b61eba8
W/Firestore(13145): (21.3.0) [FirestoreCallCredentials]: Failed to get token: com.google.firebase.FirebaseException: An internal error has occurred. [ yRequests to this API securetoken.googleapis.com method google.identity.securetoken.v1.SecureToken.GrantToken are blocked.�
W/Firestore(13145): #type.googleapis.com/google.rpc.Helpq
W/Firestore(13145): o
W/Firestore(13145):  Google developer console API keyKhttps://console.developers.google.com/project/280924879656/apiui/credential ].
I/BiChannelGoogleApi(13145): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@b61eba8
W/Firestore(13145): (21.3.0) [FirestoreCallCredentials]: Failed to get token: com.google.firebase.FirebaseException: An internal error has occurred. [ yRequests to this API securetoken.googleapis.com method google.identity.securetoken.v1.SecureToken.GrantToken are blocked.�
W/Firestore(13145): #type.googleapis.com/google.rpc.Helpq
W/Firestore(13145): o
W/Firestore(13145):  Google developer console API keyKhttps://console.developers.google.com/project/280924879656/apiui/credential ].
I/BiChannelGoogleApi(13145): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@b61eba8

The following APIs are enabled in the cloud console

enter image description here

Register through email/password also enabled in firebase

I am using these rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

I believe that is enough information. Thank you for your time.


Solution

  • Firestore security rules are not properly configured. See Get started with Cloud Firestore Security Rules.

    Since you mentioned you are implementing Firebase Authentication, you're going to want something like the example they provide:

    // Allow read/write access on all documents to any user signed in to the application
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    But note that you're probably also going to want to have multi-level administration which is done via Custom Tokens. See Create Custom Tokens and you'll probably end up with something like a rule allowing admin access to everything and then you'll narrow access from there, e.g.:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth.token.admin == true;
        }
        match /users/{userId} {
          allow read, write: if request.auth.uid == userId;
        }
      }
    }