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

如何从Firestore中加载IllnessHistory数组字段数据?

解决方案:Firestore读取IllnessHistory数组字段

问题根因

你当前的代码在构造GiveDonor对象时,对应IllnessHistory字段的入参直接硬编码传入了空ArrayList(),没有从Firestore返回的文档快照中读取对应字段值,自然无法拿到存储的病史数组数据。

修复代码

Firestore会自动将文档中的数组类型字段映射为JVM中的List类型,你只需要从document的data中取出对应字段做安全类型转换即可,不需要编写额外的特殊查询逻辑。
修复后的完整代码如下:

override suspend fun loadHistoryGiveDonor(userId: String): ArrayList<GiveDonor> {
    val data = ArrayList<GiveDonor>()

    giveDonorCollectionReference.whereEqualTo(Constants.USER_ID, userId).get()
        .addOnSuccessListener { documents ->
            for (document in documents) {
                // 安全读取IllnessHistory字段,做空值和异常兜底
                val illnessHistory = try {
                    @Suppress("UNCHECKED_CAST")
                    ArrayList(document.data[Constants.ILLNESS_HISTORY] as? List<String> ?: emptyList())
                } catch (e: ClassCastException) {
                    arrayListOf<String>()
                }

                data.add(
                    GiveDonor(
                        document.id,
                        document.data[Constants.USER_ID].toString(),
                        document.data[Constants.NAME].toString(),
                        document.data[Constants.WHATSAPP_NUMBER].toString(),
                        document.data[Constants.DATE_BIRTH].toString(),
                        document.data[Constants.GENDER].toString(),
                        document.data[Constants.BLOOD_TYPE].toString(),
                        document.data[Constants.PROVINCE].toString(),
                        document.data[Constants.CITY].toString(),
                        document.data[Constants.DISTRICT].toString(),
                        document.data[Constants.BODY_WEIGHT].toString(),
                        document.data[Constants.BODY_HEIGHT].toString(),
                        document.data[Constants.CURRENT_CONDITION].toString(),
                        document.data[Constants.LAST_DONOR_DATE].toString(),
                        document.data[Constants.EVER_COVID].toString(),
                        document.data[Constants.COVID_STATUS].toString(),
                        document.data[Constants.RECOVERED_DATE].toString(),
                        illnessHistory, // 替换原代码中的空ArrayList
                        document.data[Constants.NOTE].toString(),
                    )
                )
            }
            Log.d("ResultsizeofGive", "数据量:${data.size} 文档内容:$documents")
        }.await()
    return data
}

校验要点

  • 确认Constants类中对应IllnessHistory的常量值和Firestore控制台中显示的字段名完全一致,大小写、拼写错误都会导致字段读取失败
  • 如果IllnessHistory数组中存储的不是字符串类型,而是自定义对象,需要将类型转换处的List<String>替换为实际对应的数据类型,避免类型转换异常
  • 上述代码中增加的空值兜底、类型转换异常捕获逻辑,是为了防止字段缺失、类型不匹配时引发应用崩溃

Firestore数据结构参考:
Firestore数据结构截图

内容的提问来源于stack exchange,提问作者Eko Muhammad Rilo Pembudi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 03:42:17