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

Android模拟器连接本地Firestore模拟器为何返回空DataSnapshot?

Android模拟器连接本地Firestore模拟器查询数据为空问题

我正在测试一款基于Firebase Firestore的Android应用,遇到了本地模拟器数据查询的问题:

  • 从云端Firestore查询数据完全正常,但使用本地运行的Firestore模拟器时,查询返回空DataSnapshot
  • 本地模拟器与云端Firestore存储的数据完全一致
  • 在Android模拟器的浏览器中输入10.0.2.2:4001,可以正常进入Firebase Emulator Suite,且Firestore模拟器状态显示为On,说明连接已建立

初始化Firestore实例的代码如下:

private val db = Firebase.firestore
db.useEmulator("10.0.2.2", 8080)
db.firestoreSettings = firestoreSettings { isPersistenceEnabled = false }
val docVal = db.collection("district_codes")
            .document("district_codes")
            .get()
            .await()

本地Firestore模拟器运行日志:

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://127.0.0.1:4001/               │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ 127.0.0.1:8080 │ http://127.0.0.1:4001/firestore │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at 127.0.0.1:4401
  Other reserved ports: 4501, 9150

更新
我创建了一个简单的测试条目:在路径/Test/doc_test下添加了名为test_field的字符串字段,本地模拟器中该条目结构显示正常。

使用以下代码查询:

val docVal = db.collection("Test").document("doc_test").get().await()
val foundValue = docVal.get("test_field") as String?
Log.i(TAG_FIRESTORE, "Found following test value: ${foundValue} - ${docVal}")

得到的日志输出:

Found following test value: null - DocumentSnapshot{key=Test/doc_test, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=null}

为何我无法从本地Firestore模拟器获取数据,而云端查询却正常?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:35:56