How to remove duplicates from List of Objects

In this article, we will discuss how to remove the duplicate from the list of objects by its property value. What shall we do today is to add one object model class name as a Student which includes the list of the duplicate name? The object model class includes the name of the student roll number as well as the date of birth.

Related article: How To Implement The CheckboxListTile In Flutter

There is the various method how we can remove duplicates from the list of objects. Here we will use the concept of set in dart. Sets in Dart is a special case in List where all the inputs are unique i.e. it doesn’t contain any repeated input. It can also be interpreted as an unordered array with unique inputs. The set comes into play when we want to store unique values in a single variable without considering the order of the inputs. The sets are declared by the use of a set keyword.

There are two ways of defining the set

var variable_name = <variable_type>{};
or,
 Set <variable_type> variable_name = {};

In this example, we are going to show how we can remove duplicates from an array List of objects by matching its property value in Dart:

Object Model Class

class Student {
  String name, dob;
  int rollno;

  Student({
  required this.name, 
  required this.rollno,
  required this.dob});
}

List of Objects

 List<Student> students = [
    Student(name: "Ram Shrestha", rollno: 3, dob: "2003-10-23"),
    Student(name: "Hari Giri", rollno: 4, dob: "2005-01-13"),
    Student(name: "Ram Shrestha", rollno: 6, dob: "1993-03-15"),
    Student(name: "Harry Nakarmi", rollno: 3, dob: "2011-09-05"),
    Student(name: "Hari Giri", rollno: 4, dob: "1993-05-11"),
  ];

How to Make a List of Object Unique by String Property Value:

var set = Set<String>();
  List<Student> uniquelist =
      students.where((student) => set.add(student.name)).toList();
  print(set);

The output of the above code will be {Ram Shrestha, Hari Giri, Harry Nakarmi}.

How to Make a List of Object Unique by integer Property Value:

var set = <String>{};
  List<Student> list =
      students.where((e) => set.add(e.rollno.toString())).toList();
  print(set);

The output of the above code will be {3, 4, 6}

Now let’s take another example of how we can implement with the help of flutter:

Model Class Object

class Student {
  String name, dob;
  int rollno;

  Student({
  required this.name, 
  required this.rollno, 
  required this.dob});
}
import 'package:flutter/material.dart';
import 'package:vlogpost/model/student.dart';

class DuplicateListScreen extends StatefulWidget {
  const DuplicateListScreen({Key? key}) : super(key: key);

  @override
  State<DuplicateListScreen> createState() => _DuplicateListScreenState();
}

class _DuplicateListScreenState extends State<DuplicateListScreen> {
  @override
  Widget build(BuildContext context) {
    List<Student> students = [
      Student(name: "Ram Shrestha", rollno: 3, dob: "2003-10-23"),
      Student(name: "Hari Giri", rollno: 4, dob: "2005-01-13"),
      Student(name: "Ram Shrestha", rollno: 6, dob: "1993-03-15"),
      Student(name: "Harry Nakarmi", rollno: 3, dob: "2011-09-05"),
      Student(name: "Hari Giri", rollno: 4, dob: "1993-05-11"),
      Student(name: "Nabin Prajapati", rollno: 7, dob: "1993-05-11"),
      Student(name: "Sanam kc", rollno: 4, dob: "1993-05-11"),
    ];
    var set = <String>{};
    List<Student> duplicateName =
        students.where((student) => set.add(student.name)).toList();
    return Scaffold(
      appBar: AppBar(
        title: const Text('Removing Duplicate from list'),
        centerTitle: true,
      ),
      body: Column(
          children: duplicateName
              .map((e) => Card(
                    child: ListTile(
                      title: Text(e.name),
                      subtitle: Text(e.dob),
                      leading: CircleAvatar(
                        radius: 35,
                        child: Text(
                          e.rollno.toString(),
                          style: const TextStyle(fontSize: 25),
                        ),
                      ),
                    ),
                  ))
              .toList()),
    );
  }
}

Meanwhile, when we run this application in the emulator or device, we should get the UI similar to the screenshot below:

In this way, we can remove duplicate objects by matching their property value to make a unique list of objects in Flutter/Dart.

Thank you for reading the guide on How to Remove Duplicates from List of Objects by Property Value in Dart/Flutter.

Also Read: How To Fix The Error “Bottom Overflow By XX.XX PIXELS”?

Tags: