This is the code that im developing to get a collection from Firestore and store all its data in another map
onPressed: () async {
Map<String, dynamic> data = {};
await FirebaseFirestore.instance.collection('primary').get() .then((values) {
for (var I in values.docs) {
data.addAll(i.data());
}
print(data);
});
},
if I just print i.id, I will get all 9 ids that are available. But, by doing this to copy all the data from the map, when I print the Map data
, Im only Storting the last value. Ive tried to use forEach but happened the same
CodePudding user response:
You have to make data
a List<Map<String, dynamic>>
instead of Map<String, dynamic>
so you can add values to it instead of overwriting it the whole time.
So do this:
onPressed: () async {
List<Map<String, dynamic>> data = {};
await FirebaseFirestore.instance.collection('primary').get().then((snapshots) {
for (var doc in snapshots.docs) {
data.add(doc.data());
}
});
print(data);
},