Problem Description
In my Flutter app using Firebase, many Android users are encountering the following error:
[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.
This error typically occurs with the first (or one of the first) Firebase get queries in the app. The issue seems to be affecting multiple apps, suggesting it might be a broader problem rather than specific to one implementation.
Environment
- Flutter version: 3.24.0
- Firebase Core version: [Your Firebase Core version]
- Cloud Firestore version: [Your Cloud Firestore version]
- Affected platforms: Android
- Device/Emulator: [Specify if it’s on real devices, emulators, or both]
- Android version(s) affected: [List the Android versions where this issue occurs, if known]
Steps to Reproduce
- Initialize Firebase in the Flutter app
- Attempt to perform a Firestore get query (preferably when not signed in)
- Observe the error message in the console or as a user-facing error
Expected Behavior
The Firestore query should execute successfully, retrieving the requested data without any errors.
Actual Behavior
The app throws the “service unavailable” error, preventing the query from completing successfully.
Code Snippet
Here’s a minimal code snippet demonstrating the issue:
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:flutter/foundation.dart';
class MainFirebaseRepository {
final FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
Future<int> latestAppVersion() async {
try {
DocumentSnapshot<Map<String, dynamic>> snapshot =
await _firebaseFirestore.collection('settings').doc('latestAppVersion').get();
if (Platform.isIOS) {
return snapshot.data()?['iosBuild'] ?? 0;
} else if (Platform.isAndroid) {
return snapshot.data()?['androidBuild'] ?? 0;
}
return 0;
} catch (e) {
print('Error fetching latest app version: $e');
rethrow;
}
}
}
class AppVersionChecker {
final MainFirebaseRepository _repository = MainFirebaseRepository();
Future<void> checkIfLatestAppVersion() async {
try {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
int buildNumberStoredInFirebase = await _repository.latestAppVersion();
int myBuildNumber = int.tryParse(packageInfo.buildNumber) ?? 0;
print('Firebase build number: $buildNumberStoredInFirebase');
print('Current app build number: $myBuildNumber');
if (buildNumberStoredInFirebase > myBuildNumber) {
print('App not using latest version');
} else {
print('App is using latest version');
}
} catch (e) {
print('Error checking app version: $e');
}
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
AppVersionChecker checker = AppVersionChecker();
await checker.checkIfLatestAppVersion();
}
Additional Note
It’s important to highlight that this issue is not related to internet connectivity. The affected devices have stable and functional internet connections when the error occurs. This distinguishes our problem from similar posts where network issues were the root cause.
What I’ve Tried
1. Implementing a retry mechanism with exponential backoff
2. Checking Firebase console for any service disruptions
3. Verifying correct Firebase initialization
4. Ensuring the latest versions of Firebase plugins are used
5. Checking internet connectivity before making the query
Additional Context
• This issue seems to be more prevalent on Android devices.
• It’s not consistently reproducible, which makes debugging challenging.
• The error suggests it might be a transient condition, but it’s occurring frequently enough to impact user experience significantly.
Skintied Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.