我想從我從 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”列出 2 個串列,為“componentName”列出另一個串列。任何人都可以幫助我洗掉重復資料,我們將其添加到物件中,以便我可以在需要時使用所有資料。謝謝
編輯-:下面是模型類-
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,
};
}
uj5u.com熱心網友回復:
您可以使用removeWhere查找重復元素并將其洗掉。我使用了兩個鍵(typeName 和 Id)。
GetLaboratorComponents labModel;
List<LaboratorComponents> dataList = [];
labModel = GetLaboratorComponents.fromJson(response);
labModel.data.forEach((element) {
dataList.removeWhere((e) => element.laboratoryTypeName == e.laboratoryTypeName || element.laboratoryComponentId == e.laboratoryComponentId);
dataList.add(element);
});
labModel.data = dataList;
return labModel;
uj5u.com熱心網友回復:
以下示例代碼為“laboratoryTypeName”和“componentName”創建了一個集合:
void main() {
const data = {
"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
},
]
};
final Set<String> laboratoryTypeNames = {
for (final item in data['data'] ?? []) item['laboratoryTypeName'],
};
final Set<String> componentNames = {
for (final item in data['data'] ?? []) item['componentName'],
};
print(laboratoryTypeNames);
print(componentNames);
}
以下是如何將其添加到模型的示例。我會在GetLaboratorComponents回傳Set物件上添加吸氣劑。
(此外,我不得不稍微更新代碼,因為您似乎使用的是過時的 dart 版本,并且您的代碼無法在最新的 dart 版本中編譯)。
import 'dart:convert';
GetLaboratorComponents getLaboratorComponentsFromJson(String str) =>
GetLaboratorComponents.fromJson(json.decode(str));
String getLaboratorComponentsToJson(GetLaboratorComponents data) =>
json.encode(data.toJson());
class GetLaboratorComponents {
GetLaboratorComponents({
required this.data,
required this.exceptionInfo,
required this.message,
required this.messages,
required this.isSuccess,
});
List<LaboratorComponents> data;
dynamic exceptionInfo;
dynamic message;
dynamic messages;
bool? isSuccess;
/*
* Added getters for laboratoryTypeNames and componentNames here.
*/
Set<String> get laboratoryTypeNames => {
for (final item in data)
if (item.laboratoryTypeName != null) item.laboratoryTypeName!,
};
Set<String> get componentNames => {
for (final item in data)
if (item.componentName != null) item.componentName!,
};
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({
required this.laboratoryComponentId,
required this.laboratoryTypeId,
required this.laboratoryTypeName,
required this.componentName,
required this.uom,
required this.componentDataType,
required this.componentDataTypeValue,
required this.laboratoryTypeSequence,
required 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,
};
}
void main() {
const data = {
"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
},
]
};
final laborator = GetLaboratorComponents.fromJson(data);
print(laborator.laboratoryTypeNames);
print(laborator.componentNames);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/390018.html
