7

I am using flutter, in Android Studio.

I am getting this in my console:

W/Firestore(26053): (24.2.2) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
W/Firestore(26053): 
W/Firestore(26053): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
E/flutter (26053): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [cloud_firestore/unavailable] The service is currently unavailable. This is a most likely a transient condition and may be corrected by retrying with a backoff.
E/flutter (26053): #0      checkIfDocExists (package:crypto_app/Functions/firestorehelper.dart:29:5)
E/flutter (26053): <asynchronous suspension>
E/flutter (26053): #1      _MainPagesState._checkUsername (package:crypto_app/UI/mainpages.dart:63:21)
E/flutter (26053): <asynchronous suspension>
E/flutter (26053): 
W/Firestore(26053): (24.2.2) [WatchStream]: (a4a9ed6) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.

I have tried:

  1. put <uses-permission android:name="android.permission.INTERNET" /> in all 3 AndroidManifest.xml files
  2. put <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> in AndroidManifest.xml under main
  3. Enabled Multidex
  4. Used a different wifi connection
  5. Checked back the second day with the same issue
  6. My minimum SDK version is 21
  7. Copied my code to a fresh new project

Here is my main function:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    name: 'Retrospect',
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(MyApp());
}

I should be initializing the app correctly, because I get his in my console:

I/FirebaseApp(26053): Device unlocked: initializing all Firebase APIs for app Retrospect
I/flutter (26053): Refreshing
W/DynamiteModule(26053): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(26053): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(26053): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.

The problem could be caused by the line W/DynamiteModule(26053): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found., that I was also unable to resolve.

Here are my pubspec.yaml file dependencies:

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  firebase_core: ^1.20.1
  cloud_firestore: ^3.4.4
  http: ^0.13.4
  path_provider: ^2.0.11
  get_storage: ^2.0.3

I believe that I am using the latest versions of firebase_core and cloud_firestore.

My app/build.gradle file dependencies and plugins:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.android.support:multidex:1.0.3"
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'com.google.android.gms:play-services-ads:21.1.0'
    implementation platform('com.google.firebase:firebase-bom:30.3.2')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-storage-ktx'
}

My build.gradle file:

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.13'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

google-services.json file location:

enter image description here

Here are my rules in Firestore (rules should not be the problem because I was able to successfully read data using python.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2022, 12, 12);
    }
  }
}

Finally, here is the function I call:

import 'package:cloud_firestore/cloud_firestore.dart';

Future<bool> checkIfDocExists(String docId) async {
  try {
    // Get reference to Firestore collection
    var collectionRef = FirebaseFirestore.instance.collection('accounts');

    var doc = await collectionRef.doc(docId).get();
    return doc.exists;
  } catch (e) {
    throw e;
  }
}

What causes this error? (I am unable to access Cloud Firestore at all through flutter). I did not see my antivirus (Windows Defender) or Firewall block anything, but I can provide the logs if needed.

BX07
  • 123
  • 1
  • 10
  • 1
    everything looks fine. Did you verified your app name `Retrospect` ? – Ashutosh Patole Aug 14 '22 at 14:49
  • I'm not sure what that means, how do I verify my app name? – BX07 Aug 14 '22 at 15:04
  • Got to firebase console -> click on gear icon -> select project settings. There you will find the project name. Sorry I just found I mentioned app name earlier. It is project name :-) – Ashutosh Patole Aug 15 '22 at 03:20
  • 1
    I am having the exact same problem. Just started today, NOTHING changed on my project. I did update Android Studio though. – Damian Aug 16 '22 at 04:58
  • I couldn't fix the problem. But as an alternative, I used google cloud functions invocation to read from firestore and return the values I need. – BX07 Aug 16 '22 at 16:27
  • 2
    Found the solution. https://stackoverflow.com/questions/73370728/firebase-doesnt-work-on-android-studio-emulator/73584389#73584389 – Damian Sep 29 '22 at 13:51
  • @Damian THANK YOU SO MUCH! FINALLY SOLVED MY 1-mo LONG PROBLEM!!! – BX07 Sep 29 '22 at 20:53

3 Answers3

1

Thanks to @Damian for the solution!

The problem was the emulator, not the code itself

Read more here: Firebase doesn't work on Android Studio Emulator

How to continue with this issue:

  • If you are not using Firestore excessively, you could try accessing data through cloud functions http invocation.
BX07
  • 123
  • 1
  • 10
0

Enabling appcheck on the app helped solve the problem on my end

Crucialjun
  • 193
  • 1
  • 8
0

Switching the emulator's or device's internet connection to a stronger alternative fixed it.

Mwanagenzi
  • 136
  • 1
  • 6