In programming, lists are pretty useful to use when you need to store multiple values in a single variable. On the other hand, using list of model class to store multiple objects which in turn store various variables can be very efficient. To point out, a model class is a part of MVC architecture that help in controlling data flow within your program. MVC stands for “Model View Control” which is in fact a software design pattern used to divide the program logic into three layers. Find out more on MVC design pattern here.
Now making the list of models a unique list in flutter can be challenging so here’s how you can create flutter distinct list of models to create Flutter distinct list.
The solution is to use looping. Nested For loops can be a great solution to remove duplicates from list of models in Flutter. Create the first loop to iterate through the list items and the second loop to check for and remove duplicated objects.
What is Model Class?
Now a model is a class that determines the structure of the data where you can declare variables, data types, and even methods when you want to add some functionality. For example, a class named person contains different variables including name, gender, age, etc. Below is a sample code of a model class.
Class Person {
late String name, gender;
int age;
Person ({This.name, this.geder, this.age});
}
Let’s say you want to store data for different people. Instead of doing it in separate variables, you can create a list of type Person then add the data like the following code sample.
List<Person> people = [];
people.add(Person(name: ‘Sam’, gender: ‘male’, age: 23));
people.add(Person(name: ‘Sara’, gender: ‘female’, age: 29));
people.add(Person(name: ‘Sara, gender: ‘female, age: 29));
Sometimes you would need to get a unique list without duplicated objects. One suggested solution to remove duplicates from list is to convert that list to a set then convert it back to a list. This might work perfectly when the list is of type String or integers but not when you have a list of type Model
Making Flutter Distinct List of Models
Unfortunately, there isn’t a built function in dart that can remove duplicates from models list. You have to come up with your own logic when it comes to achieving your goal of removing duplicate items from a list of models
Below is my way of doing so in very simple steps using nested for loops.
List<Person> removeDuplicates(List<Person> people) {
//create one list to store the distinct models
List<Person> distinct;
List<Person> dummy = people;
for(int i = 0; i < people.length; i++) {
for (int j = 1; j < dummy.length; j++) {
if (dummy[i].name == people[j].name) {
dummy.removeAt(j);
}
}
}
distinct = dummy;
print(distinct);
return distinct.map((e) => e).toList();
}
Outcome
[{name: ‘Sam’, gender: ‘male’, age: 23}, { name: ‘Sara’, gender: ‘female’, age: 29}]
Explanation
The first thing I’m doing here is creating two lists (distinct and dummy). Distinct is the list that I am going to return which only consists of the unique items after removing the duplicates. Dummy is just another list that I am using to process the data to avoid manipulating the original list I am passing to the function. Another reason for using dummy list is to use in the inner loop when I’m checking against duplicate items.
In the nested loops what I’m basically doing is getting each item of the original list (outer loop) then looping through the list to check if the same item exists (inner loop) and to remove it from the list.
Conclusion
In this article, you’ve learned how to remove duplicate items from a list of model classes in Dart. This might be very helpful and beneficial if you’re seeking distinction and uniqueness in your software. Don’t hesitate to leave a comment if you have any question about this solution OR of you’ve come up with a better solution yourself 😉
Find more articles at here
where you get:
words[i].name? i don’t see any “words” on your code
Thanks for pointing this out, i never realized this typo in the post. It should be dummy[i].name