Home > Software engineering >  get specific value of Data from cloud firestore
get specific value of Data from cloud firestore

Time:09-01

I am getting my documents from cloud firestore with:

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?

I am new to cloud firestore but I hope you understand me.

CodePudding user response:

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 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
  • Related