-1

i want to remove duplicate data from the array that i am getting from api and i have tried sorting, compairing, toSet().toList() but nothing seems to work. below is the data that i am getitng -:

{
  "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
    },
  
   ]
}

here i want to make 2 list one for "laboratoryTypeName" and other for "componentName" . can anyone help how i remove duplicate data us add it to a object so that i can used all data when needed. thanks

EDIT -: below is the model 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,
  };
}


3
  • duplicates = {list name}.toSet().toList(); Commented Dec 22, 2021 at 5:04
  • tried but not working. Commented Dec 22, 2021 at 5:12
  • stackoverflow.com/questions/14665923/… Commented Dec 22, 2021 at 5:23

1 Answer 1

1

You can use removeWhere to find duplicate elements and remove it. I used two keys (typeName and 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.