Home > Software engineering >  How to return a list of map keys if they are true
How to return a list of map keys if they are true

Time:08-11

I have the following map and I need to return 2 lists.

The lists will contain the map KEYS, one with only true values and another list with only the false or null values.

Map<int, bool?> weekdaymap = {
      0: _habit.wd1,
      1: _habit.wd2,
      2: _habit.wd3,
      3: _habit.wd4,
      4: _habit.wd5,
      5: _habit.wd6,
      6: _habit.wd7,
    };

I'm new to dart/flutter and I can't find the proper way to do it.

CodePudding user response:

 List<int> trueKeys = [];
 List<int> falseKeys = [];
 weekdaymap.forEach((k, v) {
       if(v == true){
         trueKeys.add(k);
      }else if(v == false){
         falseKeys.add(k);
      }else{
         //Handle For null cases
      }
 });
  • Related