androidfirebasekotlingoogle-cloud-platformgoogle-cloud-firestore

Firestore .where + Filter.and queries not working


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:

  1. Cannot access 'where': it is package-private in 'CollectionReference'
  2. Filter.and can only be called from within the same library (com.google.firebase:firebase-firestore)
  3. Filter.inArray can only be called from within the same library (com.google.firebase:firebase-firestore)
  4. 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?


Solution

  • As long as you're using the correct dependencies and the following imports:

    import com.google.firebase.Firebase
    import com.google.firebase.firestore.Filter
    import com.google.firebase.firestore.firestore
    

    Your query should work.

    I'm trying to perform a compound and Firestore query in Kotlin.

    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 an and condition, you can simply use:

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

    For which you should create an index.