簡體   English   中英

為什么我會收到有關變量初始化的錯誤?

[英]Why am I getting this error about variable initialization?

錯誤是Variable 'uid2' must be initialized ,我已經在它說這個的地方發表了評論。 我的目標是從一個集合中查詢一個 uid,然后將其插入到另一個集合中。

fun addFriend(username: String) { 

        var uid2: String

        var docRef = firebaseFirestore.collection(collUsers).whereEqualTo("username", username)
            .get()
            .addOnSuccessListener { documents ->
                for (document in documents) {
                    Log.d(tag, "${document.id} => ${document.data}")
                    uid2 = document.data.getValue("uid").toString()
                    Log.d(tag, "uid_2: $uid2")
                }
            }
            .addOnFailureListener { exception ->
                Log.w(tag, "Error getting documents: ", exception)
            }

        var collRelationships2: CollectionReference = firebaseFirestore.collection(collRelationships)
        var relationshipsMap: HashMap<String, Any> = hashMapOf(
                                        "uid_1" to firebaseAuth.uid.toString()
                                        , "uid_2" to uid2 //Error is here
                                        , "stat" to 1
                                        , "createDate" to FieldValue.serverTimestamp()
                                        , "modifiedDate" to FieldValue.serverTimestamp()
                                    )

        collRelationships2.add(relationshipsMap)

    }

get()是異步的,這意味着在從 firestore 檢索數據之前將執行另一個任務。 在您的代碼中,hash map 的創建是在從數據庫中檢索數據之前發生的,因此您會收到未初始化uid2的錯誤。 您可以執行以下操作:

        var docRef = firebaseFirestore.collection(collUsers).whereEqualTo("username", username)
            .get()
            .addOnSuccessListener { documents ->
                for (document in documents) {
                    Log.d(tag, "${document.id} => ${document.data}")
                    uid2 = document.data.getValue("uid").toString()
                    Log.d(tag, "uid_2: $uid2")

                var collRelationships2: CollectionReference = firebaseFirestore.collection(collRelationships)
                var relationshipsMap: HashMap<String, Any> = hashMapOf(
                                        "uid_1" to firebaseAuth.uid.toString()
                                        , "uid_2" to uid2 //Error is here
                                        , "stat" to 1
                                        , "createDate" to FieldValue.serverTimestamp()
                                        , "modifiedDate" to FieldValue.serverTimestamp()
                                    )

        collRelationships2.add(relationshipsMap)
                }
            }
            .addOnFailureListener { exception ->
                Log.w(tag, "Error getting documents: ", exception)
            }

另一種方法是使用await() ,這將使代碼非常簡單。

這里要注意的是 Firestore 查詢是異步的,並且會在查詢完成之前立即返回。 稍后在主線程上使用查詢結果調用成功和錯誤回調。

您的代碼正在嘗試使用uid2 ,然后它最終會在未來的回調中被賦予一個值。 如果要繼續處理查詢結果,處理這些結果的代碼必須在回調本身內,而不是在回調外。

(例外情況是,如果您重寫代碼以使用協程,但這不在此問題的 scope 范圍內。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM