【问题标题】:Why am I getting this error about variable initialization?为什么我会收到有关变量初始化的错误?
【发布时间】:2020-12-22 18:39:31
【问题描述】:

错误是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)

    }

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    get() 是异步的,这意味着在从 firestore 检索数据之前将执行另一个任务。在您的代码中,哈希映射的创建是在从数据库中检索数据之前发生的,因此您会收到 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(),这将使代码非常简单。

    【讨论】:

    【解决方案2】:

    这里要注意的是,Firestore 查询是异步的,并且会在查询完成之前立即返回。稍后在主线程上使用查询结果调用成功和错误回调。

    您的代码正在尝试使用uid2,然后它最终会在未来的回调中被赋予一个值。如果您想继续处理查询结果,处理这些结果的代码必须在回调本身内,而不是在回调外。

    (例外情况是如果您重写代码以使用协程,但这超出了本问题的范围。)

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2019-12-28
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多