[英]How do I get the DocumentID of a firestore document that has a particular field value in flutter?
[英]How do i get the single field value from querysnapshot in flutter firestore
实际上,我需要访问我的 firestore 集合文档中名为 userid 的字段,但我无法访问它。 所以,有人能帮我解决这个问题吗?打印语句只打印 json 中的数据。
void logIn(String email, String password) async {
if (_formKey.currentState!.validate()) {
FirebaseFirestore.instance
.collection("users")
.where("username", isEqualTo: email)
.where("password", isEqualTo: password)
.get()
.then((QuerySnapshot querySnapshot) => {
if (querySnapshot.docs.isNotEmpty)
{
print(querySnapshot.docs.first.data()),
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) =>
HomeScreen(userId: querySnapshot.docs.first.id),
这是我得到的结果。 但我只需要 userid 字段:
在DocumentSnapshot
上调用data()
,会为您提供该文档中的所有字段。 不幸的是,它作为通用类型T?
,这可能是您正在努力的地方。
要获取特定字段,您需要将data()
转换为Map
,然后查找该字段:
var data = querySnapshot.docs.first.data() as Map;
var value = data["userId"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.