我是Flutter的新手。我尝试使用类,但是由于参数是变量,所以我看不到如何在foreach循环中将新值应用于User对象的参数。
我的班级用户:
class UsersList{
List<User> users;
UsersList({
this.users
});
factory UsersList.fromJson(List<dynamic> json) {
List<User> users= List<User>();
users= json.map((i) => User.fromJson(i)).toList();
return UsersList(
users : users
);
}
Map<String, dynamic> toJson() => {
"users": users,
};
@override
String toString() {
return '{'
'users: ${this.users},'
'}';
}
}
class User{
int type;
String name;
bool gender;
num distance;
num index;
num position;
num fuel;
num values
User({
this.type,
this.name,
this.gender,
});
factory User.fromJson(Map<String, dynamic> json){
return User(
type : json['type'],
name: json['name'],
gender : json['gender'],
distance: json['distance'],
index: json['index'],
position: json['position'],
fuel: json['fuel'],
values: json['values'],
);
}
Map<String, dynamic> toJson() => {
"type" : type,
"name" : name,
"gender" : gender,
"distance" : distance,
"index" : index,
"position" : position,
"fuel" : fuel,
"values" : values,
};
@override
String toString() {
return '{'
'type : ${this.type},'
'name: ${this.name},'
'gender : ${this.gender},'
'distance: ${this.distance},'
'index: ${this.index},'
'position: ${this.position},'
'fuel: ${this.fuel},'
'values: ${this.values},'
'}';
}
}
这是我的功能:
static User rebuildUser(User item, UsersList usersJson, FormCalculator formData)
{
if(usersJson== null || usersJson.users.isEmpty) return null;
final User _rebuild = User();
List<User> _allUsersType = usersJson.users.where((c) => (c.type == item.type)).toList();
User_upperItem = _allUsersType.firstWhere((c) => (c.distance > item.distance));
item.toJson().forEach((key, value) {
if (key != "type" && key != 'distance' && key != 'index')
{
final _newValue = CalculatorInitial.calculateNew(item, _upperItem, formData.distance, "distance", key);
_rebuild.toJson()[key] = _newValue; // ----- This not work ! -----
}
});
return _rebuild; // The values of my User rebuild are unchanged because all of which are null
}
此功能使我可以使用2位用户之间计算出的数据来重建用户。 此功能有效,因为我得到了预期的结果。我没有看到如何在用户对象中应用这些新值。我使用“ toJson()”函数能够将键作为变量使用,但是似乎没有将新值应用于我的对象。
答案 0 :(得分:0)
也许可行
(".gallery").mouseenter(() => {
$(this).css("gray");
$(this).append('<div id="preview"');
// console.log($(this).attributes);
// $(this).append($(`<img src="${`images/medium/"${$(this).attr('id')}"`}"
// alt="${$(this).attr('title')}"/>`
// ));
$(this).append('</div>')
})