[英]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.