skip to Main Content

can someone helpe me, please? i don’t know why isn’t working

main() {
  var students = [
    {'name': 'Mateus', 'grade': 9.9},
    {'name': 'Pedro', 'grade': 9.3},
    {'name': 'Paulo', 'grade': 8.7},
    {'name': 'João', 'grade': 8.1},
    {'name': 'Tiago', 'grade': 7.6},
    {'name': 'Bartolomeu', 'grade': 6.8},
  ];

  Function(Map) onlyName = (student) => students['names'];
  var names = students.map(onlyName);
  print(names);
}

3

Answers


  1. If I understand correctly, you need to use

    Function(Map) onlyName = (student) => student['name'];
    

    to get names.

    Login or Signup to reply.
  2. Because your grade in name model is String, but you insert int value.

    you can insert value in ‘ ‘.

    Also, you define the name wrong.

    you must change ‘names’ to ‘name’ in function

    should be:

    Function(Map) onlyName = (student) => students['name'];
    
    Login or Signup to reply.
  3. The error appears because students is an array of type Map. In your onlyName map function you try to get an element at index ‘name’, which is invalid. students[0] would be valid.

    But your logic is still wrong. As i understood you try to get all names from your Maps.

    You want to iterate through the elements, that’s why you defined the function. The parameter student contains the current student. Since this is a map, you can simply call student[‘name’] so that it returns the name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search