skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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 of implementation 'com.google.firebase:firebase-firestore-ktx'


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search