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

Firestore中where结合Filter.and查询报错,寻求解决方法

Firestore Kotlin复合查询IDE权限错误解决

问题场景

按照Firestore官方指南编写Kotlin复合AND查询代码,代码如下:

val query =
    Firebase.firestore
        .collection("profiles")
        .where(
            Filter.and(
                Filter.inArray("profileGender", preferenceGenderArray),
                Filter.equalTo("profilePaused", false)
            )
        )
        .get()

但Android Studio报出以下权限类IDE错误:

  • 无法访问'where':它在'CollectionReference'中是包私有
  • Filter.and仅能从同一库(com.google.firebase:firebase-firestore)内部调用
  • Filter.inArray仅能从同一库内部调用
  • Filter.equalTo仅能从同一库内部调用

已添加依赖:implementation 'com.google.firebase:firebase-firestore-ktx'


解决方法

1. 检查并升级Firestore依赖版本

这些错误核心原因是使用的Firestore版本过低——Filter类的公开API以及接收Filter参数的where方法,是在较新的Firestore版本中才对外开放的。

  • 直接指定最新稳定版依赖:
    implementation 'com.google.firebase:firebase-firestore-ktx:24.10.0' // 替换为当前最新稳定版
    
  • 推荐使用Firebase BoM统一管理版本(避免版本冲突):
    先在项目级build.gradle中添加BoM依赖,再在模块级依赖中省略版本号:
    // 项目级build.gradle
    dependencies {
        implementation platform('com.google.firebase:firebase-bom:32.7.0') // 最新BoM版本
    }
    
    // 模块级build.gradle
    dependencies {
        implementation 'com.google.firebase:firebase-firestore-ktx'
    }
    

2. 简化查询写法(可选)

Firestore默认支持多where调用实现AND逻辑,无需使用Filter.and,这种写法更简洁且不会触发权限问题:

val query = Firebase.firestore
    .collection("profiles")
    .whereIn("profileGender", preferenceGenderArray)
    .whereEqualTo("profilePaused", false)
    .get()

3. 同步并重建项目

修改依赖后,点击Android Studio工具栏的「Sync Project with Gradle Files」按钮同步依赖,再执行「Build -> Clean Project -> Rebuild Project」确保变更生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 18:15:05