I have collection Tools that has multiple documents with shared attributes and one of them being toolName, I would like to count the tools that have the same name and return their value. Meaning if 2 documents have "toolName = Screen Reader" then the count would be 2 https://i.stack.imgur.com/vEmYZ.png
This is what I tried so far, however it is returning 4 as length but there's only 2 "Screen Readers" in the documents
https://i.stack.imgur.com/oa7nB.png
CodePudding user response:
You can count all the tool names like this:
FirebaseFirestore.instance.collection('Tools').get().then((value) {
var toolNamesCounted = Map();
List toolNames = [];
value.docs.forEach((data) {
var doc = data.data();
toolNames.add(doc['toolName']);
});
toolNames.forEach((e) {
if (!toolNamesCounted.containsKey(e)) {
toolNamesCounted[e] = 1;
} else {
toolNamesCounted[e] = 1;
}
});
print(toolNamesCounted);
// result should be:
// {Screen Reader: 2, ...etc }
});