在 flutter 中从大数据 api 中删除重复数据

remove duplicate data from large data api in flutter

我想从我从 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
    },
  
   ]
}

这里我想制作 2 个列表,一个用于“laboratoryTypeName”,另一个用于“componentName”。 任何人都可以帮助我如何删除重复数据我们将其添加到对象以便我可以在需要时使用所有数据。 谢谢

编辑-: 下面是模型 class-

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,
  };
}


您可以使用 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;