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

Android Kotlin中如何从Firestore获取String类型数组streamers?

Kotlin 获取Firestore中String数组的解决方案

问题背景

在Firestore的Agencies集合中,每个文档包含streamers字符串数组字段,尝试用Kotlin获取该数组时,出现类型不匹配错误:

Type mismatch. Required: kotlin.collections.ArrayList /* = java.util.ArrayList */ Found: Any?

原尝试代码:

firebaseFireStore.collection("Agencies")
        .addSnapshotListener { snapshot, e ->
            if (e == null) {
                val documents = snapshot?.documents
                if(documents != null) {
                    val list = mutableListOf<Ajanslar>()
                    for (document in documents) {

                        val agencyName = document.get("agencyName").toString()
                        val coverImage = document.get("coverImage").toString()
                        val owner = document.get("owner").toString()
                        val platform = document.get("platform").toString()
                        val streamers = document.get("streamers")
                        val newAjans = Ajanslar(agencyName,coverImage,owner,platform,streamers)
                        list.add(newAjans)
                    }
                    ajansListRepo.value = list
                }
            }
        }

错误原因

Firestore的document.get()方法返回的是Any?类型,而Ajanslar构造函数要求传入ArrayList<String>类型,直接赋值会触发类型不匹配,且未处理null情况。

解决方案

1. 安全类型转换(推荐)

使用Kotlin的as?操作符进行安全转换,转换失败时返回默认空数组,避免崩溃:

val streamers = document.get("streamers") as? ArrayList<String> ?: arrayListOf()
  • as?:尝试将Any?转换为ArrayList<String>,失败则返回null
  • ?::Elvis操作符,当左侧为null时返回右侧的空数组

2. 使用泛型重载的get()方法

Firestore提供了泛型版本的get()方法,可以直接指定目标类型:

val streamers = document.get("streamers", ArrayList::class.java) ?: arrayListOf()

如果需要更严格的类型校验(指定String泛型),可以借助TypeToken:

val streamers = document.get("streamers", object : TypeToken<ArrayList<String>>() {}.type) ?: arrayListOf()

3. 数据类自动映射(更优雅)

如果Ajanslar是数据类,可直接用toObject()方法让Firestore自动解析所有字段(包括数组),无需手动逐个取值:
首先定义数据类(给默认值避免null问题):

data class Ajanslar(
    val agencyName: String = "",
    val coverImage: String = "",
    val owner: String = "",
    val platform: String = "",
    val streamers: ArrayList<String> = arrayListOf()
)

然后简化代码:

firebaseFireStore.collection("Agencies")
    .addSnapshotListener { snapshot, e ->
        if (e == null) {
            val documents = snapshot?.documents ?: return@addSnapshotListener
            // 自动映射并过滤解析失败的文档
            val list = documents.mapNotNull { it.toObject(Ajanslar::class.java) }.toMutableList()
            ajansListRepo.value = list
        }
    }

优化后的完整代码(安全转换版本)

firebaseFireStore.collection("Agencies")
    .addSnapshotListener { snapshot, e ->
        if (e == null) {
            val documents = snapshot?.documents ?: return@addSnapshotListener
            val list = mutableListOf<Ajanslar>()
            for (document in documents) {
                val agencyName = document.get("agencyName").toString()
                val coverImage = document.get("coverImage").toString()
                val owner = document.get("owner").toString()
                val platform = document.get("platform").toString()
                val streamers = document.get("streamers") as? ArrayList<String> ?: arrayListOf()
                val newAjans = Ajanslar(agencyName, coverImage, owner, platform, streamers)
                list.add(newAjans)
            }
            ajansListRepo.value = list
        }
    }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 17:25:29