简体   繁体   English

从 Cloud Firestore 获取特定数据值

[英]get specific value of Data from cloud firestore

I am getting my documents from cloud firestore with:我正在从 Cloud Firestore 获取我的文档:

QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("section").get();
final allData = querySnapshot.docs.map((doc) => doc.data()).toList();

However this provides me a list with Items like但是,这为我提供了一个包含类似项目的列表

{startTime: 9:54:34, stopTime: 10:34:34, date: 22.23.2323, duration: 324234, weekday: Mo}

This is not a json string, so how do I only get the startTime for example, or even better, how do I get a List with objects?这不是 json 字符串,所以我如何仅获取 startTime ,甚至更好,如何获取包含对象的列表?

I am new to cloud firestore but I hope you understand me.我是 Cloud Firestore 的新手,但我希望你能理解我。

You can access to items inside your list like this:您可以像这样访问列表中的项目:

print('${allData[0][startTime]}'); // 9:54:34

I recommended to you use class model like this:我建议您像这样使用 class model :

class itemModel {
 final String startTime;
 final String stopTime;
 final String date;
 itemModel({@required this.startTime,@required 
   this.stopTime,@required this.date});


 static itemModel fromJson(Map<String, dynamic> json){
   return itemModel(
        startTime:json[startTime] as String,
        stopTime:json[stopTime] as String,
        date:json[date] as String);
 }
}

and use it like this:并像这样使用它:

List<itemModel> allData = querySnapshot.docs.map((doc) => itemModel.fromJson(doc.data())).toList();

print("${allData[0].startTime}"); // 9:54:34

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM