kotlin 如何从firebase读取更新的值?

cclgggtu  于 2023-01-17  发布在  Kotlin
关注(0)|答案(1)|浏览(120)

I am trying to read a String field that I recently updated. My function that should return the String, returns an empty String.
Here is my codes:
Function that returns the updated String:

fun readUsersOfficeHoursList(email: String, callback: (String) -> Unit) {
    val database = FirebaseFirestore.getInstance()
    val ref = database.collection("Users").document(email)

    ref.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                val officeHoursList = document.get("office_hours_list") as String
                callback(officeHoursList)
                Log.d(TAG, "office_hours_list successfully read")
            } else {
                Log.d(TAG, "Is empty")
                callback("")
            }
        }
        .addOnFailureListener { exception ->
            if (exception is FirebaseFirestoreException) {
                Log.e(TAG, "Error getting document: ", exception)
            }
            callback("")
        }
}

Function that updates the field:

fun updateUserOfficeHoursList(email: String, code: String){
    val database = FirebaseFirestore.getInstance()
    val ref = database.collection("Users").document(email)
    var list = ""

    ref.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                list = document.get("office_hours_list") as String? ?: ""
                Log.d(TAG, "office_hours_list successfully read")
                if(!list.contains(code)){
                    if (list.isEmpty()){
                        list = code
                    }
                    else{
                        list = "$list, $code"
                    }
                    ref.update("office_hours_list", list)
                        .addOnSuccessListener { Log.d(TAG, "List successfully updated") }
                        .addOnFailureListener { e -> Log.w(TAG, "Error updating list", e) }
                }else{
                    Log.d(TAG, "code already in the list")
                }
            } else {
                Log.d(TAG, "Is empty")
            }
        }
        .addOnFailureListener { exception ->
            if (exception is FirebaseFirestoreException) {
                Log.e(TAG, "Error getting document: ", exception)
            }
        }
}

My test code:

myClass.updateUserOfficeHoursList("tfh@gmail.com", "1VVNFxSGbYaauk3iLV80, 
1a79bhnaJsY5OhHwaYhH")
        myClass.readUsersOfficeHoursList("tfh@gmail.com") {fieldValue1 ->
            textView.text = fieldValue1
            Log.d(TAG, "fieldValue1: $fieldValue1")
        }

The error I get:

**2023-01-16 13:43:50.523 8529-8529/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 8529 java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String at com.example.myapplication.RepositoryMockup$readUsersOfficeHoursList$1.invoke(RespositoryMockup.kt:239) at com.example.myapplication.RepositoryMockup$readUsersOfficeHoursList$1.invoke(RespositoryMockup.kt:237) at com.example.myapplication.RepositoryMockup.readUsersOfficeHoursList$lambda$15(RespositoryMockup.kt:237) at com.example.myapplication.RepositoryMockup.$r8$lambda$Rz1CeV4qQ243JiYTVZ8j2Ijj1y0(Unknown Source:0) at com.example.myapplication.RepositoryMockup$$ExternalSyntheticLambda16.onSuccess(Unknown Source:2) at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7898) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) **

gblwokeq

gblwokeq1#

这是检查您试图读取的文档是否存在的错误方法:

if (document != null) {

当不存在文档时,Firestore API返回一个DocumentSnapshot(因此不是null),其exists属性设置为false

if (document.exists) {

相关问题