Malay Agrawal
Malay Agrawal

Reputation: 47

How Acess Document Snapshot elements?

I am accessing a document from firebase using the command FirebaseFirestore.instance.collection("Collection").doc(a).get(); Can I accesses the elements without knowing their name and only by index number?

Upvotes: 2

Views: 188

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12353

DocumentSnapshot response = await FirebaseFirestore.instance.collection("Collection").doc(a).get(); 

Will return a DocumentSnapshot object.

To access this object, which is returned in the form of a Map<String, dynamic>, you need to call data() on it, i.e:

Map<String, dynamic> resultsMap = response.data();

After this, it's basic Map manipulation. If you don't know the keys of this map, you can map your map into a List, and use the indexes then.

Incase you are wondering how to turn a map to a list,

List newList = resultsMap.entries.map((e) =>  e.value).toList();
print(newList[0]); //should print the value of what you need.

Upvotes: 4

Related Questions