How to Remove Duplicates from List of Objects by Property Value in Dart/Flutter

In this example, we are going to show you how to remove duplicates from an array List of objects by matching it property value in Dare or Flutter. See the example below:

class Student{
   String name, dob;
   int rollno;

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

List<Student> students = [
    Student(name: "John Cena", rollno: 3, dob: "2003-10-23"),
    Student(name: "John Cena", rollno: 4, dob: "2005-01-13"),
    Student(name: "Jack Sparrow", rollno: 6, dob: "1993-03-15"),
    Student(name: "Harry Potter", rollno: 3, dob: "2011-09-05"),
    Student(name: "Jack Sparrow", rollno: 4, dob: "1993-05-11"),
];

var seen = Set<String>();
List<Student> uniquelist = students.where((student) => seen.add(student.name)).toList();
//output list: John Cena, Jack Sparrow, Harry Potter

var seen = Set<String>();
List<Student> uniquelist = students.where((student) => seen.add(student.rollno.toString())).toList();
//output: John Centa (3), John Cena (4), Jack Sparrow (6)

import 'package:flutter/material.dart';

void main() {
  runApp( MaterialApp(
       home: Home()
  ));
}

class Home extends  StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {

  @override
  Widget build(BuildContext context) {

    List<Student> students = [
        Student(name: "John Cena", rollno: 3, dob: "2003-10-23"),
        Student(name: "John Cena", rollno: 4, dob: "2005-01-13"),
        Student(name: "Jack Sparrow", rollno: 6, dob: "1993-03-15"),
        Student(name: "Harry Potter", rollno: 3, dob: "2011-09-05"),
        Student(name: "Jack Sparrow", rollno: 4, dob: "1993-05-11"),
    ];

    var seen = Set<String>();
    List<Student> uniquelist = students.where((student) => seen.add(student.name)).toList();
    //output list: John Cena, Jack Sparrow, Harry Potter

    return Scaffold(
         appBar: AppBar(
            title: Text("Remove Duplicate Objects"),
            backgroundColor: Colors.redAccent,
         ),
          body: Container(
             alignment: Alignment.center,
             padding: EdgeInsets.all(20),
             child: Column(
               children:uniquelist.map((studentone){
                  return Container(
                    child: Card(
                       child:ListTile(
                          leading: Text(studentone.rollno.toString(), 
                                    style: TextStyle(fontSize: 25),),
                          title: Text(studentone.name),
                          subtitle: Text(studentone.dob),
                       )
                    )
                  );
               }).toList(),
             ),
          )
      );
  }
}

class Student{
   String name, dob;
   int rollno;

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

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

No any Comments on this Article


Please Wait...