Home > Mobile >  How to get the length of map values flutter
How to get the length of map values flutter

Time:10-05

Map<String, bool> _toDos = {
      'emailVerified': true,
      'medicationProfile': false,
      'pastIllnessHistory': false,
      'medicationHistory': false,
      'allergies' : false

    };

Say I have a map object like this and I want to get the length of strings that contains "true" values. How do I do so with flutter?

_toDos.length gives me the length of the map but I want to get the length of items that contain only "true" values.

CodePudding user response:

_todos.values.where((element) => element == true).length

CodePudding user response:

void main() {
 Map<String, bool> _toDos = {
      'emailVerified': true,
      'medicationProfile': false,
      'pastIllnessHistory': false,
      'medicationHistory': false,
      'allergies' : false

    };
  int count = 0;
  _toDos.values.forEach((value) {
    if(value== true){
      count =1;
    }
  });
  print(count); //1
}
  • Related