Flutter 仅当json不存在时,才会向其添加数据
我有下面的型号Flutter 仅当json不存在时,才会向其添加数据,flutter,Flutter,我有下面的型号 product\u model.dart class ProductModel { String status; String message; List<Results> results; ProductModel({this.status, this.message, this.results}); ProductModel.fromJson(Map<String, dynamic> json) { status = j
product\u model.dart
class ProductModel {
String status;
String message;
List<Results> results;
ProductModel({this.status, this.message, this.results});
ProductModel.fromJson(Map<String, dynamic> json) {
status = json['status'];
message = json['message'];
if (json['data'] != null) {
results = new List<Results>();
json['data'].forEach((v) {
results.add(new Results.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
data['message'] = this.message;
if (this.results != null) {
data['data'] = this.results.map((v) => v.toJson()).toList();
}
return data;
}
}
class Results {
String id;
String productCode;
String category;
String title;
String isActive;
Results(
{this.id,
this.productCode,
this.category,
this.title,
this.isActive,
});
Results.fromJson(Map<String, dynamic> json) {
id = json['id'];
productCode = json['product_code'];
category = json['category'];
title = json['title'];
isActive = json['is_active'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['product_code'] = this.productCode;
data['title'] = this.title;
data['category'] = this.category;
data['is_active'] = this.isActive;
return data;
}
}
类产品模型{
字符串状态;
字符串消息;
列出结果;
ProductModel({this.status,this.message,this.results});
ProductModel.fromJson(映射json){
status=json['status'];
message=json['message'];
if(json['data']!=null){
结果=新列表();
json['data'].forEach((v){
results.add(newresults.fromJson(v));
});
}
}
映射到JSON(){
最终地图数据=新地图();
数据['status']=this.status;
数据['message']=此.message;
if(this.results!=null){
data['data']=this.results.map((v)=>v.toJson()).toList();
}
返回数据;
}
}
课堂成绩{
字符串id;
字符串产品代码;
字符串类别;
字符串标题;
字符串是活动的;
结果(
{this.id,
这个.productCode,
这一类,,
这个名字,
这是非常活跃的,
});
Results.fromJson(映射json){
id=json['id'];
productCode=json['product_code'];
category=json['category'];
title=json['title'];
isActive=json['is_active'];
}
映射到JSON(){
最终地图数据=新地图();
数据['id']=this.id;
数据['product_code']=this.productCode;
数据['title']=this.title;
数据['category']=this.category;
数据['is_active']=this.isActive;
返回数据;
}
}
我有一个将产品保存到收藏夹的功能。收藏夹将另存为文件中的json
import 'package:example/utils/favstorage.dart';
import 'package:example/models/product_model.dart';
class FavoriteProducts {
FavoritesStorage storage = FavoritesStorage();
List<ProductModel> favorites = [];
Future addFavorite(ProductModel products) async {
favorites.add(products);
await storage.writeFavorites(favorites);
}
}
import'package:example/utils/favstorage.dart';
导入“包:示例/models/product_model.dart”;
类偏好产品{
FavoriteStorage存储=FavoriteStorage();
列出收藏夹=[];
Future addFavorite(ProductModel产品)异步{
收藏。添加(产品);
等待存储。writeFavorites(收藏夹);
}
}
我只想在产品不存在的情况下将其添加到收藏夹中。如何更新addFavorite
方法,以便如果特定的id
不存在,则只继续添加到收藏夹。
我是个新手。有人能帮我吗???您可以使用和索引where在列表中搜索具有相同id的项目,如下所示:
Future addFavorite(ProductModel products) async {
if(favorites.indexWhere((listProduct) => listProduct.id == products.id) == -1){
favorites.add(products);
await storage.writeFavorites(favorites);
}
}
-1表示没有项目,如果有项目,它会从列表中返回该项目。您可以使用和索引where在列表中搜索具有相同id的项目,如下所示:
Future addFavorite(ProductModel products) async {
if(favorites.indexWhere((listProduct) => listProduct.id == products.id) == -1){
favorites.add(products);
await storage.writeFavorites(favorites);
}
}
-1表示没有项目,如果有项目,它会从列表中返回该项目。了解您的型号:
Future addFavorite(ProductModel products) async {
bool containsId = favorites.any((ProductModel model){
return model.results.any((Results result){
return products.results.any((Results resultInProducts) => resultInProducts.id == result.id);
});
});
if(!containsId){
favorites.add(products);
await storage.writeFavorites(favorites);
}
}
我希望这有帮助,如果有任何疑问,请评论。如果这个答案对你有帮助,那么请接受并投票决定答案。了解你的模型:
Future addFavorite(ProductModel products) async {
bool containsId = favorites.any((ProductModel model){
return model.results.any((Results result){
return products.results.any((Results resultInProducts) => resultInProducts.id == result.id);
});
});
if(!containsId){
favorites.add(products);
await storage.writeFavorites(favorites);
}
}
我希望这有帮助,如果有任何疑问,请评论。如果这个答案对你有帮助,那么请接受并向上投票。如果答案不在列表中,则答案应为-1。文档:“如果找不到元素,则返回-1。”不起作用,错误:没有为类“ProductModel”定义getter“id”。我现在看到您的ProductModel没有id。如果没有id,如何标识每个产品?您真的想添加结果吗?有点混乱。谢谢@WatsMyName我不知道从何处获得0。在
ProductModel
中有一个结果类,所有产品信息都在该类中。请看我在帖子中的第一个代码。THanksIt不在列表中时应为-1。文档:“如果找不到元素,则返回-1。”不起作用,错误:没有为类“ProductModel”定义getter“id”。我现在看到您的ProductModel没有id。如果没有id,如何标识每个产品?您真的想添加结果吗?有点混乱。谢谢@WatsMyName我不知道从何处获得0。在ProductModel
中有一个结果类,所有产品信息都在该类中。请看我在帖子中的第一个代码。谢谢,我还没有机会测试你的代码,我现在不在家,我会接受你的答案,等我回来再检查。好的,希望它对你有用。。如果有任何疑问,请评论..我还没有机会测试你的代码,我不在家,我会接受你的答案,我会在回来后检查。好的,希望它对你有用。。如有任何疑问,请发表意见。。