一、map 联合.. 操作符更改数组中的某一个字段,其余字段不变
void main() {
List<PersonModel> list =[PersonModel(name:'kobe1',age:20,sex:1),
PersonModel(name:'kobe2',age:30,sex:2),
PersonModel(name:'kobe3',age:40,sex:1),
];
// map 操作会扭转原来的 List
list.map((e)=>e..name = 'new name').toList();
for(var item in list){print(item.toJson());
}
// 打印输出
// {name: new name, age: 20, sex: 1}
// {name: new name, age: 30, sex: 2}
// {name: new name, age: 40, sex: 1}
}
class PersonModel {
String name;
int age;
int sex;
PersonModel({this.name, this.age, this.sex});
PersonModel.fromJson(Map<String, dynamic> json) {name = json['name'];
age = json['age'];
sex = json['sex'];
}
Map<String, dynamic> toJson() {final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['age'] = this.age;
data['sex'] = this.sex;
return data;
}
}