Home > Back-end >  Flutter Firestore\ How to get field value from map under array
Flutter Firestore\ How to get field value from map under array

Time:12-04

enter image description hereenter image description here

I develop an application by using Flutter and firestore. But now I having trouble, I don't know how to get the field value(underlined in red),I have read the Firebase Documentation but it's still confused me.

My English is pretty limited,please forgive me.

CodePudding user response:

With Firestore, you first read the entire document, and then get the fields you want on the client (in this case Flutter).

For example:

FirebaseFirestore.instance
.collection('playroom')
.get()
.then((QuerySnapshot querySnapshot) {
    querySnapshot.docs.forEach((doc) {
        print(doc["Material_List"][0]["X1"]);
    });
});

This prints all of the first "X1" values in the playroom collection.

If you want a specific document, do this:

FirebaseFirestore.instance
.collection('playroom')
.doc("ID OF THE DOCUMENT")
.get()
.then((DocumentSnapshot doc) {
  print(doc.data()["Material_List"][0]["X1"]);
});
  • Related