在 Firestore 中执行复合查询时出错

Error while performing compound queries in firestore

我想在 firestore 中执行复合查询,我想在其中获取字段 bloodgroup 等于 A+ 且字段 createdBy 不等于 [=14] 的所有文档=].此电子邮件是登录用户的电子邮件。当我执行查询时,我得到 NullPointerException。如何正确执行查询 021-07-24 19:50:24.746 17550-17550/com.example.bloodbankcompany E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bloodbankcompany, PID: 17550 java.lang.NullPointerExceptionatcom.example.bloodbankcompany.UserlistActivity$EventChangeListener3.onEvent(UserlistActivity.kt:217) 我将文档快照存储在 userArrayList 数组中。没有 whereNotEqualTo 查询,我得到的输出是我的文档在 recyclerview 中列出的位置。

private fun EventChangeListener2(){

        val sharedPreferences1 = getSharedPreferences("email", Context.MODE_PRIVATE)
        val email: String? = sharedPreferences1.getString("email","null")?.trim()

        Toast.makeText(this, "ssrae$email", Toast.LENGTH_SHORT ).show()

        mFireStore.collection("applicationForm").whereNotEqualTo("createdBy",email).whereEqualTo("bloodgroup","A+").addSnapshotListener(object : EventListener<QuerySnapshot>{
            override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
                if (error!= null){
                    Log.e("firestore error", error.message.toString())
                }

                for(dc: DocumentChange in value?.documentChanges!!){
                    if (dc.type== DocumentChange.Type.ADDED){


                        userArrayList.add(dc.document.toObject(User1::class.java))
                        var number=userArrayList
                        var number1 =userArrayList

                    }
//                    Toast.makeText(applicationContext,userArrayList.toString(), Toast.LENGTH_SHORT).show()

                }
                myAdapter.notifyDataSetChanged()
            }

        })

    }

好吧,我已经编辑了一些你的代码,如果它仍然不起作用,请添加评论。

此外,下面评论了有关更改的说明。

private fun EventChangeListener2() {

        val sharedPreferences1 = getSharedPreferences("email", Context.MODE_PRIVATE)
        val email: String? = sharedPreferences1.getString("email", "null")?.trim()

        Log.d("firestore email", email.toString())

        Toast.makeText(this, "ssrae$email", Toast.LENGTH_SHORT).show()

        // try and catch will avoid your app to crash.
        try {

            //ref
            var ref = mFireStore.collection("applicationForm")
                .whereEqualTo("bloodgroup", "A+")


            /**
             *  I believe since your email is of type nullable and there may be
             *  maybe a chance that email is null and is given to whereNotEqualTo
             *  I am just making an assumption here since I don't know what you
             *  recieve from sharedPreferences and whether it is null or not
             *
             *  So, what I have done here is,
             *  firstly, I have split the firestore call into 2 parts and
             *  Secondly, I have a null-check for email, if it is
             *  not-null ref will also include this query
             *
             */
            //null Check for email
            if (email != null) ref = ref.whereNotEqualTo("createdBy", email)

            // Snapshot Listener
            ref.addSnapshotListener(object : EventListener<QuerySnapshot> {
                override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
                    if (error != null) {
                        Log.e("firestore error", error.message.toString())
                    }

                    for (dc: DocumentChange in value?.documentChanges!!) {
                        if (dc.type == DocumentChange.Type.ADDED) {


                            userArrayList.add(dc.document.toObject(User1::class.java))
                            var number = userArrayList
                            var number1 = userArrayList

                        }
//                    Toast.makeText(applicationContext,userArrayList.toString(), Toast.LENGTH_SHORT).show()

                    }
                    myAdapter.notifyDataSetChanged()
                }

            })
        } catch (e: Exception) {
            Log.e("firestore error", "Error", e)
        }
    }

编辑: 根据 firebase 文档,

https://firebase.google.com/docs/firestore/query-data/indexing#exemptions

如果您尝试使用未映射到现有索引的范围子句进行复合查询,则会收到错误消息。错误消息包括直接 link 在 Firebase 控制台中创建丢失的索引。