Home > Back-end >  How to compare two Lists of Objects By Property Value
How to compare two Lists of Objects By Property Value

Time:10-02

I've two Lists Of myContactsModal in Object i.e

List<myContactsModel> allContacts=[myContactsModel(displayName: "ABC",
                phoneNumbers: "1234"),myContactsModel(displayName: "EFG",
                phoneNumbers: "12345"),myContactsModel(displayName: "Test",
                phoneNumbers: "78923")];
List<myContactsModel> chekList =[myContactsModel(displayName: "ABC",
                phoneNumbers: "8973"),myContactsModel(displayName: "BHGS",
                phoneNumbers: "12347872")];

What I want to achieve is compare these two lists and if element in checkLists having same displayName exists in allContacts array i want to update that element in allContacts List and If it doesnt exists in allContacts list Then It should add new entry in allContacts List. The Output Should Be:

List<myContactsModel>allContacts=[
 myContactsModel(displayName: "ABC",
            phoneNumbers: "8973"),
 myContactsModel(displayName: "EFG",
            phoneNumbers: "12345"),
 myContactsModel(displayName: "Test",
            phoneNumbers: "78923"),
 myContactsModel(displayName: "BHGS",
            phoneNumbers: "12347872")
]

CodePudding user response:

To compare to the list in Dart we need to use this function

import 'package:flutter/foundation.dart';

if(listEquals(allContacts, chekList)){
    print('List is Equal');
}else{
    print('List is not Equal');
}

This will not work correctly in one case if your model class doesn't override have equality.

This means myContactsModel class must override hashCode and ==.

A sample model class with equality. You can find override hashCode and == and that must be also in your model class.

class myContactsModel {
  final int id;
  final String name;
  final String email;

  myContactsModel({
    required this.id,
    required this.name,
    required this.email,
  });

  // This must be there
  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is myContactsModel &&
        other.id == id &&
        other.name == name &&
        other.email == email;
  }

  // This must be there
  @override
  int get hashCode {
    return id.hashCode ^ name.hashCode ^ email.hashCode;
  }
}

CodePudding user response:

Please try this.It should work .I have updated your Model class name to MyContactsModel

List<MyContactsModel> allContacts=[MyContactsModel(displayName: "ABC",
                    phoneNumbers: "1234"),MyContactsModel(displayName: "EFG",
                    phoneNumbers: "12345"),MyContactsModel(displayName: "Test",
                    phoneNumbers: "78923")];
    List<MyContactsModel> chekList =[MyContactsModel(displayName: "ABC",
                    phoneNumbers: "8973"),MyContactsModel(displayName: "BHGS",
                    phoneNumbers: "12347872")];
      
      chekList.forEach((element){
        MyContactsModel existInAllContact=allContacts.firstWhere((item)=>item.displayName==element.displayName,orElse: () => null);
        if(existInAllContact==null)
          ///Add this 
        {
          allContacts.add(existInAllContact);
          
        }
        ///update
        else{
          allContacts.removeWhere((item)=>item.displayName==element.displayName);
          allContacts.add(element);
          
        }
        
        
      }
      );
    }
  • Related