【发布时间】:2020-06-02 19:22:06
【问题描述】:
为什么我不能在像这样映射时引用一个已经放在另一个对象的键中的对象中的键:
var object = {
"allStudents": [
{
"name": "John",
"hobbies": {
"sport": "Football",
"music": "Piano"
} ,
},
{
"name": "Julyo",
"hobbies": {
"sport": "Vollyball",
"music": "Guitar"
} ,
},
],
};
class Student {
String name;
dynamic hobbies;
Student({this.name, this.hobbies});
}
class Hobbies {
String sport;
String music;
Hobbies({this.sport, this.music});
}
List<Student> studentsList = [];
void getStudentsList () {
studentsList = object["allStudents"].map((stu)=>
Student(
name: stu["name"] ,
hobbies: Hobbies(
sport: stu['hobbies']['sport'] ,
music:stu['hobbies']['music'],
) ,
)
).toList();
}
在我添加第二个运算符 ['sport'] 或 ['music'] 之前,一切都运行良好,因为 IDE 一直告诉我:
没有为“Object”类定义运算符“[]”。 尝试定义运算符'[]'。
【问题讨论】:
-
尝试将单引号 ' 更改为双引号 " ?
-
没用,我不认为引号是问题的一部分。