I have a model class called Media, and I want to check if two lists of Media objects are equal. Below is the code for my Media model class and two example lists.
import 'package:equatable/equatable.dart';
class Media extends Equatable {
final int id;
final String title;
Media({required this.id, required this.title});
@override
List<Object> get props => [id, title];
}
void main() {
List<Media> list1 = [
Media(id: 1, title: 'Media One'),
Media(id: 2, title: 'Media Two'),
];
List<Media> list2 = [
Media(id: 1, title: 'Media One'),
Media(id: 2, title: 'Media Two'),
];
// How to compare list1 and list2 for equality?
}
I want to compare list1 and list2 to see if they contain the same elements with the same attributes, but the order of items in the lists does not matter. How can I do this?
Thank you for your help!
2
Answers
Try the following snippet of code:
Try below code hope its help to you. use toSet method for this. If you set the list vice-versa it also gives the status