Issue
This Content is from Stack Overflow. Question asked by Mat Koffman
I am getting this error message in my flutter app:
The method ‘forEach’ isn’t defined for the type ‘Function’.
Try correcting the name to the name of an existing method, or defining a method named ‘forEach’.
I am using this code:
getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
List<ElementTask> listElement = new List(), listElement2;
Map<String, List<ElementTask>> userMap = new Map();
List<String> cardColor = new List();
if (widget.user.uid.isNotEmpty) {
cardColor.clear();
snapshot.data.docs.map<List>((f) {
String color;
f.data.forEach((a, b) {
if (b.runtimeType == bool) {
listElement.add(new ElementTask(a, b));
}
if (b.runtimeType == String && a == "color") {
color = b;
}
});
listElement2 = new List<ElementTask>.from(listElement);
for (int i = 0; i < listElement2.length; i++) {
if (listElement2.elementAt(i).isDone == false) {
userMap[f.data()] = listElement2;
cardColor.add(color);
break;
}
}
if (listElement2.length == 0) {
userMap[f.data()] = listElement2;
cardColor.add(color);
}
listElement.clear();
}).toList();
What can I do to fix it?
Solution
you can put () after data so it will be:
f.data().forEach
because data itself is not a map to apply forEach on it but data() will return a map so you can call forEach on it..
This Question was asked in StackOverflow by Mat Koffman and Answered by wafaa sisalem It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.