You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用网络正常却无法使用Firebase Cloud Firestore求助

配置Flutter Cloud Firestore时遇到连接不可用问题

问题场景

严格按照官方文档配置Flutter应用的Firebase Cloud Firestore,初始化和数据获取代码如下,但调用获取数据方法时触发cloud_firestore/unavailable错误,无法连接Firestore后端。

初始化代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
}

数据获取代码

Future getData() async {
    final data = await FirebaseFirestore.instance
    .collection("vinland-saga")
    .doc("HgeWMoAs5IbIg8HzipTV")
    .get();
}

错误日志

Performing hot reload...
Syncing files to device sdk gphone x86...
Reloaded 1 of 859 libraries in 1,897ms.
W/DynamiteModule(11596): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(11596): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(11596): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
I/ansk.wbijam_ap(11596): The ClassLoaderContext is a special shared library.
I/ansk.wbijam_ap(11596): The ClassLoaderContext is a special shared library.
I/TetheringManager(11596): registerTetheringEventCallback:com.jansk.wbijam_app
V/NativeCrypto(11596): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 295 native methods...
W/ansk.wbijam_ap(11596): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (greylist, reflection, allowed)
I/ProviderInstaller(11596): Installed default security provider GmsCore_OpenSSL
W/Firestore(11596): (24.2.2) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
W/Firestore(11596): 
W/Firestore(11596): 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 (11596): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] 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 (11596): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (11596): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter (11596): <asynchronous suspension>
E/flutter (11596): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:367:43)
E/flutter (11596): <asynchronous suspension>
E/flutter (11596): #3      MethodChannelDocumentReference.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_reference.dart:68:42)
E/flutter (11596): <asynchronous suspension>
E/flutter (11596): #4      _JsonDocumentReference.get (package:cloud_firestore/src/document_reference.dart:146:7)
E/flutter (11596): <asynchronous suspension>
E/flutter (11596): #5      _HomeState.getData (package:wbijam_app/main.dart:27:18)
E/flutter (11596): <asynchronous suspension>
E/flutter (11596): 
W/Firestore(11596): (24.2.2) [WriteStream]: (578340) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
W/DynamiteModule(11596): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
W/Firestore(11596): (24.2.2) [WatchStream]: (2c09279) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
I/DynamiteModule(11596): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(11596): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.

已完成的排查操作

  • 已在所有AndroidManifest.xml文件中添加网络权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
  • 关闭防火墙,确认设备网络连接正常
  • 验证设备支持Google Play Services且配置正确
  • 提升minSDK版本,在gradle中启用multiDexEnabled

可能的解决方案

  • 检查Firebase配置文件:确认google-services.json已正确放置在Android项目的app目录下,文件内容与Firebase控制台生成的一致,无项目ID、API密钥等字段遗漏或错误。
  • 降级Firestore依赖版本:当前使用的24.2.2版本可能存在兼容性问题,可在pubspec.yaml中指定较低的稳定版本(如cloud_firestore: ^4.8.0),执行flutter pub get重新安装依赖后重试。
  • 调整模拟器网络配置:如果使用Android模拟器,尝试切换网络类型(如Wi-Fi转蜂窝网络)或重启模拟器,部分模拟器会出现网络连接异常的情况。
  • 验证Firestore安全规则:临时将安全规则设置为允许所有读写(测试完成后务必改回安全规则),排除规则拦截导致的问题:
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
  • 清除应用缓存并重装:在设备上清除应用缓存和数据,重新安装应用,避免旧配置残留影响连接。
  • 检查代理设置:确认系统或模拟器未设置代理,或代理可正常访问Firebase服务。

内容的提问来源于stack exchange,提问作者jasiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:05:17