Home > database >  Custom dart function that accepts map as argument
Custom dart function that accepts map as argument

Time:12-19

Creating a custom function that accepts a map as an arugment and print the values of the map as a list. Expected output: [98, 95, 77, 89, 74, 99, 97]`

void main() {
  Map studentMarks = {
    'James': 98,
    'Peter': 95,
    'Alson': 77,
    'Deng': 89,
    'Diing': 74,
    'Madunt': 99,
    'Kwaje': 97
  };
  List marks = getScores(studentMarks);
  print(marks);
}

dynamic getScoresList(Map map) {
  map.entries.map((ele) => print(ele.value));
}

CodePudding user response:

You may try this:

void main() {
  Map studentMarks = {
    'James': 98,
    'Peter': 95,
    'Alson': 77,
    'Deng': 89,
    'Diing': 74,
    'Madunt': 99,
    'Kwaje': 97
  };
  List marks = getScoresList(studentMarks);
  print(marks);
}

List getScoresList(Map map) {
  return map.values.toList();
}
  • Related