简体   繁体   English

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

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

So I want to retrieve a particular Document from a Collection in Firestore and assign the document to a variable so that i can use it in other activity.所以我想从 Firestore 中的集合中检索特定文档并将文档分配给变量,以便我可以在其他活动中使用它。

Collection contains Users as documents.集合包含用户作为文档。

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

Model class: USER模型类别:USER

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

You can get your user with using code below, then you can do whatever you want with it.您可以使用下面的代码获取您的用户,然后您可以用它做任何您想做的事。

       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.

相关问题 Angular Firestore - 获取文档数据并分配给变量 - Angular Firestore - Get document data and assign to variable 如何在一次又一次地点击提交按钮后通过生成不同的文档 ID 将数据添加到 firebase 火库? Android Kotlin - How to add data to the firebase firestore by generating different document id after hitting submit button again and again? Android Kotlin 如何从 Firestore 获取对文档数组的引用 - How to Get Reference to Document Array from Firestore 如何检查在 Java 中的 Firestore 上删除了哪个文档 - Android Studio - How to check which document is deleted on Firestore in Java - Android Studio 如何从 Firestore 数据库中检索字符串数组并显示它 - flutter - how to retrieve an array of strings from Firestore database and display it - flutter 如何使用 flutter 和 dart 从 Cloud Firestore 检索特定数据? - how to retrieve specific data from Cloud Firestore using flutter and dart? 如何从 Firestore 优化读取的集合中检索所有文档? - How to retrieve all documents from a collection in Firestore optimizing reads? 如何在已经从 Firestore KOTLIN 获取 arrayList 的片段上实现 SearchView - How to implement SearchView on fragment that is already fetching arrayList from firestore KOTLIN 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据? - How to display data from Firestore in a RecyclerView with Android? 如何从 Firbase Firestore 获取 Flutter 中的最后一个文档? - How to get Last document in Flutter from Firbase Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM