flutter - 如何在 Flutter 中更新复杂模型中的值?
问题描述
在我的颤振应用程序中,我有 5 个用于构建的参数 Likeeleveator,storeroom,parking,buildAge,rentPrice
这些参数的默认值一开始是 0 ,我想ApartemanRentOptionModel
在不同的步骤中更新这个类中的每个值而不更改其他值,最后将完整的值发送到服务器。
我有一个像这样的出租Apartemans选项课程:
class ApartemanRentOptionModel {
ApartemanRentOptionModel({
this.eleveator,
this.storeroom,
this.parking,
this.buildAge,
this.rentPrice
});
bool eleveator;
bool storeroom;
bool parking;
List<BuildAge> buildAge;
List<RentPrice> rentPrice;
factory ApartemanRentOptionModel.fromJson(Map<String, dynamic> json) => ApartemanRentOptionModel(
eleveator: json["eleveator"],
storeroom: json["storeroom"],
parking: json["parking"],
buildAge: List<BuildAge>.from(json["buildAge"].map((x) => BuildAge.fromJson(x))),
rentPrice: List<RentPrice>.from(json["rentPrice"].map((x) => RentPrice.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"eleveator": eleveator,
"storeroom": storeroom,
"parking": parking,
"buildAge": List<dynamic>.from(buildAge.map((x) => x.toJson())),
"rentPrice": List<dynamic>.from(rentPrice.map((x) => x.toJson())),
};
}
class BuildAge {
BuildAge({
this.buildAgeId,
this.buildAgeTitle,
this.buildAgeValue,
});
String buildAgeId;
String buildAgeTitle;
int buildAgeValue;
factory BuildAge.fromJson(Map<String, dynamic> json) => BuildAge(
buildAgeId: json["buildAgeID"],
buildAgeTitle: json["buildAgeTitle"],
buildAgeValue: json["buildAgeValue"],
);
Map<String, dynamic> toJson() => {
"buildAgeID": buildAgeId,
"buildAgeTitle": buildAgeTitle,
"buildAgeValue": buildAgeValue,
};
}
class RentPrice {
RentPrice({
this.rentPriceId,
this.rentPriceTitle,
this.rentPriceValue,
});
String rentPriceId;
String rentPriceTitle;
double rentPriceValue;
factory RentPrice.fromJson(Map<String, dynamic> json) => RentPrice(
rentPriceId: json["rentPriceID"],
rentPriceTitle: json["rentPriceTitle"],
rentPriceValue: json["rentPriceValue"].toDouble(),
);
Map<String, dynamic> toJson() => {
"rentPriceID": rentPriceId,
"rentPriceTitle": rentPriceTitle,
"rentPriceValue": rentPriceValue,
};
}
我需要更改某些数据中的值,例如BuildAge
或RentPrice
使用这样的函数:
ApartemanRentOptionModel _currentApartemanData;
changeCurretAparemanData(newdata) {
_currentApartemanData.toJson().update("BuildAge", (value) => newdata)
notifyListeners();
return null;
}
但它不起作用,没有任何变化,请帮助我如何在几次内更新单个模型类的不同值。谢谢
解决方案
您可以在模型中使用 getter 或 setter 函数 .getter 函数用于从模型中获取值,而 setter 用于在模型中设置或更新值。
推荐阅读
- c++ - 当我将它与 add_subdirectory 结合使用时,cmake 的 find_package 不起作用
- c# - System.Text:使用泛型的 JsonSerializer.Deserialize
- javascript - JavaScript 代码执行但无法运行完整脚本且没有错误
- python - 在 requests.post 中将 json-array 解析为“文件”无法作为字符串发送
- java - 根据方向旋转 tileBitmap
- node.js - mongodb中的类别和子类别使用猫鼬
- python - cv2.mouseCallback 没有在 cv2 窗口上注册鼠标点击
- jestjs - NestJS / GraphQL e2e 测试错误:“PickObjectType”的“id”字段的默认值不匹配导致的错误。确保这些 val
- flutter - CircularNotchedRectangle 类的解释
- node.js - 如何使用 Nginx 和 Node.js 映射原始 tcp 连接?