简体   繁体   English

如何使用 Map 中的键获取值<string, dyanmic>在 Flutter</string,>

[英]How to get value with key from a Map<String, dyanmic> in Flutter

I am new to flutter and Firestore, and still leaning.我是 flutter 和 Firestore 的新手,还在学习。

I have been trying to print a single value from a Map<String, Dynamic>.我一直在尝试从 Map<String, Dynamic> 打印单个值。 When I print the whole map it works but when tried to print a single value with key, it produces a null error.当我打印整个 map 时,它可以工作,但是当尝试使用键打印单个值时,它会产生 null 错误。

getUsersFromFireStore(String appUid) async {
    List<Map<String, dynamic>> list = [];
    await instance.collection('Users').where('refDocument', isEqualTo: appUid).get()
        .then((querySnapshotOne) {
      return querySnapshotOne.docs[0].reference.collection('Data').orderBy('timeStamp', descending: true).get();
    }).then((querySnapshotTwo) {
      if(querySnapshotTwo.size > 0){
        querySnapshotTwo.docs.forEach((doc) {
          String id = doc.id;
          list.add(doc.data()); // Here is the problem
          // print(doc.data().toString()); produces {age:24, name: Hari Ram}
          // But doc.data()['name'] doesn't work, gives null error
        });
      }
    });
    return list;
}

Please help.请帮忙。 Thanks in advance.提前致谢。

** UPDATE ** when tried with doc.data['name'] as per the comments, it shows following error: ** 更新 ** 根据评论尝试使用doc.data['name']时,它显示以下错误:

在此处输入图像描述

doc.data['name'] // This should solve your problem doc.data['name'] // 这应该可以解决您的问题

Remove () after the data keyword.去掉data关键字后面的()。 ie print(doc.data['name'])print(doc.data['name'])

UPDATE: Try doc.get('name')更新:试试doc.get('name')

暂无
暂无

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

相关问题 如何从 firebase 数据库 flutter 中的 map 获取值 - how to get value from map in firebase database flutter 如何更新 map 值(表类型<string> )在实时数据库(Flutter)中?</string> - how to update a map value (of type table<String>) in Realtime db (Flutter)? 如何创建和读取Flutter中的键值对数组(map)用于Firestore中存储数据 - How to create and read an array of key value pairs (map) in Flutter for storing data in Firestore 如何从firebase中的值中获取key - How to get the key from the value in firebase 如何转换 Map<string, dynamic> 至 Map<int, string> 在 flutter</int,></string,> - How to convert Map<String, dynamic> to Map<int, String> in flutter Flutter:如何解决值类型 Object 无法分配给 Map<string, dynamic> 在流构建器上</string,> - Flutter: How to solve value type Object can't be assigned to Map<String, dynamic> on streambuilder 如何从Flutter下面的JSON数据中取出key“folderName”的值? - How to retrieve the value of key "folderName" from the following JSON data in Flutter? 如何使用 flutter 中的子值从 firebase 获取数据? - How to get data from firebase using child value in flutter? 如何从 stream 获取更新 Flutter DropdownButton 中的值? - How to get Value to update in Flutter DropdownButton from stream? 如何使用futurebuilder/streambuilder(flutter)从firestore获取数组/地图并使用lisview显示 - How to get array/map from firestore and display with lisview using futurebuilder/streambuilder(flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM