Flutter + Firebase: “[cloud_firestore/unavailable] The service is currently unavailable” error on Android

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

  1. Initialize Firebase in the Flutter app
  2. Attempt to perform a Firestore get query (preferably when not signed in)
  3. 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.

New contributor

Skintied Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật