Download StackFood Multi Restaurant, Food Delivery App with Laravel Admin and Restaurant Panel Buy Full App on $79
Download FlutKit - Flutter UI Kit Most beautiful Flutter UI Kit with 20+ Full App, 450+ Screens. Buy Full Kit on $39
Download Fluxstore Multi Vendor WooCommerce/Dokan Plugin compatible WordPress eCommerce Flutter App. Buy Full App on $79

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:

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: "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"),
];

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

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

How to Make List of Object Unique by Integer Property Value:

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)

Full Flutter/Dart Example:

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

Output Screenshot:

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...