Home > Blockchain >  The method 'forEach' isn't defined for the type 'Function'. Try correcting
The method 'forEach' isn't defined for the type 'Function'. Try correcting

Time:09-19

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();

enter image description here

What can I do to fix it?

CodePudding user response:

Try to change this:

f.data.forEach((a, b) {

to this:

f.data().forEach((a, b) {

CodePudding user response:

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..

  • Related