Home > Blockchain >  Flutter how to access each field of of doc
Flutter how to access each field of of doc

Time:02-05

I am new to Flutter and I want to extract each field of each doc in order to manipulate the data.

This is the code for the extraction from the Firebase Firestore

Future<void> queryBasedOnDrug(
  String nameOfThePharmacy, String nameOfTheDrug) async {
var docSnapshot = await firestoreInstance
    .collection('Pharmacies')
    .doc(nameOfThePharmacy)
    .collection('Drugs')
    .where('Name', isEqualTo: nameOfTheDrug)
    .get()
    .then((querySnapshot) {
  querySnapshot.docs.forEach((result) {
    print(result.data());
  });
});

}

And this is the output, as you can see I have two docs that correspond to my query, and for each doc, I want to extract /access each field. enter image description here It is possible? If so How can I do that?

I search online but the things I found did not fix my problem.

CodePudding user response:

result.data() returns type Map<String, dynamic>

Map<String, dynamic> resultData = result.data();

To access your specific field you can do like this:

resultData["Price"]

For example this would print your specific field.

print(resultData["Price"]);
  •  Tags:  
  • Related