简体   繁体   English

我如何从 flutter firestore 中的 querysnapshot 获取单个字段值

[英]How do i get the single field value from querysnapshot in flutter firestore

actually I need to get access to the field called userid which in my firestore collection document but I cant access it.实际上,我需要访问我的 firestore 集合文档中名为 userid 的字段,但我无法访问它。 so, please can anyone help me with this problem?the print statement only prints the data in json..所以,有人能帮我解决这个问题吗?打印语句只打印 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),

this is the out put I get.这是我得到的结果。 but i need only the userid field:但我只需要 userid 字段:

Calling data() on a DocumentSnapshot , gives you all the fields from that document.DocumentSnapshot上调用data() ,会为您提供该文档中的所有字段。 It unfortunately does so as a generic type T?不幸的是,它作为通用类型T? , which is probably where you are struggling. ,这可能是您正在努力的地方。

To get a specific field, you'll want to cast the data() to a Map and then look up that field:要获取特定字段,您需要将data()转换为Map ,然后查找该字段:

var data = querySnapshot.docs.first.data() as Map;
var value = data["userId"];

暂无
暂无

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

相关问题 如何获取在 flutter 中具有特定字段值的 Firestore 文档的 DocumentID? - How do I get the DocumentID of a firestore document that has a particular field value in flutter? 如何将来自 Firestore 的字段值存储在 Flutter 中? - How to store field value from firestore in Flutter? 如何从列表中分别获取字段名称和字段值<object?>从 Firestore flutter</object?> - How To Get Field Name And Field Value Separately From List<Object?> From Firestore flutter 如何从 Firestore flutter 的参考字段中获取值并在 stream 构建器中显示? - How to get value from reference field in firestore flutter and display in stream builder? 如何从 flutter 的 firestore 中的一个字段发送和接收两种类型的数据? - How can I send and receive two types of data from one single field in firestore for flutter? Firestore Flutter:获取 querySnapshot.length 并设置新文档 - Firestore Flutter : get querySnapshot.length & set new doc 如何将特定字段值从 Firestore 保存到 Flutter - How to save specific field value from Firestore to Flutter 如何在 flutter 中获取 firestore 文档的 documentid? - How do I get documentid of a firestore document in flutter? Flutter/Firestore - 错误:“列表”类型的值<string> Function(QuerySnapshot)' 不能分配给 'List' 类型的变量<dynamic> '</dynamic></string> - Flutter/Firestore - Error: A value of type 'List<String> Function(QuerySnapshot)' can't be assigned to a variable of type 'List<dynamic>' 如何在 QuerySnapshot 中获取集合 - How can I get a collection inside a QuerySnapshot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM