2

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")
]

2 Answers 2

4

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;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.