I’m trying to perform a compound and
Firestore query in Kotlin by following this guide:
My code is as follows:
val query =
Firebase.firestore
.collection("profiles")
.where(
Filter.and(
Filter.inArray("profileGender", preferenceGenderArray),
Filter.equalTo("profilePaused", false)
)
)
.get()
Android Studio is giving me these errors in the IDE before I can run this:
- Cannot access ‘where’: it is package-private in ‘CollectionReference’
- Filter.and can only be called from within the same library (com.google.firebase:firebase-firestore)
- Filter.inArray can only be called from within the same library (com.google.firebase:firebase-firestore)
- Filter.equalTo can only be called from within the same library (com.google.firebase:firebase-firestore)
In my build.gradle(app) I have implementation 'com.google.firebase:firebase-firestore-ktx
What am I doing wrong here?
2
Answers
Seems as though I was using an older module.
I was using
implementation platform('com.google.firebase:firebase-bom:31.1.1')
I upgraded to
implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
Then migrated to using the Kotlin extensions (KTX) APIs in the main modules per this link: https://firebase.google.com/docs/android/kotlin-migration#ktx-apis-to-main-how-to-migrate
So using
implementation 'com.google.firebase:firebase-firestore'
instead ofimplementation 'com.google.firebase:firebase-firestore-ktx'
As long as you’re using the correct dependencies and the following imports:
Your query should work.
If you only want to create an
and
query, according to the official documentation regarding compound (AND) queries, there is no need to use a Filter with anand
condition, you can simply use:For which you should create an index.