I cannot access my firebase/firecloud data from my flutter project. I get: Could not reach Cloud Firestore backend. Connection failed 1 times

Viewed 500

I have been stuck with this for days now, I have difficulty accessing my firebase database from my flutter project. I have followed all the instructions to adding firebase to an android app on flutter and tried many suggested solutions on the web but I'm still stuck. I hope someone will be patient or kind enough to help me out. I have put all the necessary snippets below.

My dependencies

    dependencies:
      flutter:
        sdk: flutter
      google_maps_flutter: ^2.0.6
      proj4dart: ^2.0.0
      location: ^4.2.0
      cloud_firestore: ^2.5.1
      firebase_core: ^1.6.0

my imports

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

My firestore structure enter image description here

my function to retrieve the data from firestore

  Future getControls() async{
      await Firebase.initializeApp();

    FirebaseFirestore firestoreInstance = FirebaseFirestore.instance;

    QuerySnapshot data =  await firestoreInstance.collection('Controls').get();

  }

Error received enter image description here

My google service JSON file

enter image description here

My Buld.gradle file enter image description here

**

I may not be doing it right, or there is something I'm missing. Can someone please help me out .... mmmhh. Thanks in Advance

**

2 Answers
  1. First include firebase_core in your dependencies firebase_core: ^1.3.0.
  2. The screenshot you sent is firebase Realtime database (rtdb). But what you are trying to reach is cloud firestore. Did you setup cloud firestore? Confirm by going to firebase console and clicking cloud firestore by the left. Check if it is setup and if you have any data in the 'controls' collection.
  3. Go to the link given in 'errors screenshot' that you posted and enable cloud firestore api.

NB: Cloud firestore and Realtime database are 2 different firebase databases. You can visit this link to learn about their differences and decide which one to use.

After following the instructions listed above by @Peter O.

I changed the rules of my cloud firestore as follows:

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

   }
  }
}

This resolved the "Permission denied issues"

Related