繁体   English   中英

如何从 Firestore 检索文档并将其分配给变量? [安卓] [科特林]

[英]How to retrieve a document from Firestore and assign it to a variable? [ANDROID] [KOTLIN]

所以我想从 Firestore 中的集合中检索特定文档并将文档分配给变量,以便我可以在其他活动中使用它。

集合包含用户作为文档。

**FirebaseFirestore.getInstance().collection(Users).document(getCurrentUserID()).get() ....**

模型类别:USER

fun getCurrentUserID(): String {
    var currentUser = FirebaseAuth.getInstance().currentUser
    var currentUserID = ""
    if (currentUser != null) {
        currentUserID = currentUser.uid
    }
    return currentUserID
}

您可以使用下面的代码获取您的用户,然后您可以用它做任何您想做的事。

       FirebaseFirestore.getInstance()
            .collection("Users")
            .document(getCurrentUserID())
            .get()
            .addOnSuccessListener { document ->
                val user = document.toObject(USER::class.java)!!
                // Now we stored information to user variable.
                // You can use it by simply calling user.name etc.
            }
            .addOnFailureListener { e ->
                Log.e(
                    this.javaClass.simpleName,
                        "Error while getting user details.",
                        e
                )
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM