Alex97
Alex97

Reputation: 466

Flutter how to access each field of of doc

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.

Upvotes: 0

Views: 111

Answers (1)

Yanni TheDeveloper
Yanni TheDeveloper

Reputation: 406

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"]);

Upvotes: 2

Related Questions