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
If I understand correctly, you need to use
to get names.
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:
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.