【发布时间】:2022-01-23 09:35:52
【问题描述】:
我想从我从 api 获取的数组中删除重复数据,我尝试过排序、比较、toSet().toList(),但似乎没有任何效果。 以下是我正在获取的数据-:
{
"data":[
{
"laboratoryComponentId": 16,
"laboratoryTypeId": 18,
"laboratoryTypeName": "Profile1",
"componentName": "LP-PLA2 Enzyme",
"uom": "U/L",
"componentDataType": "Numeric",
"componentDataTypeValue": null,
"laboratoryTypeSequence": 1,
"laboratoryComponentSequence": 16
},
{
"laboratoryComponentId": 17,
"laboratoryTypeId": 18,
"laboratoryTypeName": "Profile1",
"componentName": "CRP C Reactive Protein",
"uom": "mg/dl",
"componentDataType": "Numeric",
"componentDataTypeValue": null,
"laboratoryTypeSequence": 1,
"laboratoryComponentSequence": 17
},
{
"laboratoryComponentId": 18,
"laboratoryTypeId": 25,
"laboratoryTypeName": "Profile2",
"componentName": "Anti TPO (Anti Micro Somal) Antibody",
"uom": "IU/ML",
"componentDataType": "Numeric",
"componentDataTypeValue": null,
"laboratoryTypeSequence": 2,
"laboratoryComponentSequence": 18
},
{
"laboratoryComponentId": 19,
"laboratoryTypeId": 25,
"laboratoryTypeName": "Profile2",
"componentName": "FT3",
"uom": "pg/ml",
"componentDataType": "Numeric",
"componentDataTypeValue": null,
"laboratoryTypeSequence": 2,
"laboratoryComponentSequence": 19
},
{
"laboratoryComponentId": 30,
"laboratoryTypeId": 8,
"laboratoryTypeName": "Profile3",
"componentName": "Fg3",
"uom": "pg/ml",
"componentDataType": "Numeric",
"componentDataTypeValue": null,
"laboratoryTypeSequence": 3,
"laboratoryComponentSequence": 30
},
]
}
在这里,我想为“laboratoryTypeName”和“componentName”创建 2 个列表。 任何人都可以帮助我如何删除重复数据,我们将其添加到对象中,以便我可以在需要时使用所有数据。 谢谢
编辑-: 下面是模型类-
import 'dart:convert';
GetLaboratorComponents getLaboratorComponentsFromJson(String str) => GetLaboratorComponents.fromJson(json.decode(str));
String getLaboratorComponentsToJson(GetLaboratorComponents data) => json.encode(data.toJson());
class GetLaboratorComponents {
GetLaboratorComponents({
this.data,
this.exceptionInfo,
this.message,
this.messages,
this.isSuccess,
});
List<LaboratorComponents> data;
dynamic exceptionInfo;
dynamic message;
dynamic messages;
bool isSuccess;
factory GetLaboratorComponents.fromJson(Map<String, dynamic> json) => GetLaboratorComponents(
data: List<LaboratorComponents>.from(json["data"].map((x) => LaboratorComponents.fromJson(x))),
exceptionInfo: json["exceptionInfo"],
message: json["message"],
messages: json["messages"],
isSuccess: json["isSuccess"],
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(data.map((x) => x.toJson())),
"exceptionInfo": exceptionInfo,
"message": message,
"messages": messages,
"isSuccess": isSuccess,
};
}
class LaboratorComponents {
LaboratorComponents({
this.laboratoryComponentId,
this.laboratoryTypeId,
this.laboratoryTypeName,
this.componentName,
this.uom,
this.componentDataType,
this.componentDataTypeValue,
this.laboratoryTypeSequence,
this.laboratoryComponentSequence,
});
int laboratoryComponentId;
int laboratoryTypeId;
String laboratoryTypeName;
String componentName;
String uom;
dynamic componentDataType;
String componentDataTypeValue;
int laboratoryTypeSequence;
int laboratoryComponentSequence;
factory LaboratorComponents.fromJson(Map<String, dynamic> json) => LaboratorComponents(
laboratoryComponentId: json["laboratoryComponentId"],
laboratoryTypeId: json["laboratoryTypeId"],
laboratoryTypeName: json["laboratoryTypeName"],
componentName: json["componentName"],
uom: json["uom"] == null ? null : json["uom"],
componentDataType: json["componentDataType"],
componentDataTypeValue: json["componentDataTypeValue"] == null ? null : json["componentDataTypeValue"],
laboratoryTypeSequence: json["laboratoryTypeSequence"],
laboratoryComponentSequence: json["laboratoryComponentSequence"],
);
Map<String, dynamic> toJson() => {
"laboratoryComponentId": laboratoryComponentId,
"laboratoryTypeId": laboratoryTypeId,
"laboratoryTypeName": laboratoryTypeName,
"componentName": componentName,
"uom": uom == null ? null : uom,
"componentDataType": componentDataType,
"componentDataTypeValue": componentDataTypeValue == null ? null : componentDataTypeValue,
"laboratoryTypeSequence": laboratoryTypeSequence,
"laboratoryComponentSequence": laboratoryComponentSequence,
};
}
【问题讨论】:
-
重复 = {列表名称}.toSet().toList();
-
试过但没用。
标签: flutter dart duplicates