0

I want to remove duplicated Profilemodel by name only in items list , so I should get NO duplicated items in screen. I want to remove duplicated Profilemodel by name only in items list , so I should get NO duplicated items in screen.

I used items.toSet().toList() but its not work with my need.

What I want

What I DONT want

Full code :

import 'package:flutter/material.dart';

void main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Application name
      title: 'Flutter Hello World',
      // Application theme data, you can set the colors for the application as
      // you want
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      // A widget which will be started on application startup
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
List<Profilemodel> items = [
  Profilemodel( age: 20 , name: 'Ahmed' ) ,
  Profilemodel( age: 30 , name: 'Fatma' ),
  Profilemodel( age: 15 , name: 'Ahmed' ),
  Profilemodel( age: 15 , name: 'jon' ),
  Profilemodel( age: 25 , name: 'Fatma' )];
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body : ListView.builder(
itemBuilder: (context, index){
  return ListTile(
    title: Text('${items[index].name}'),
  );
},
          itemCount: items.length,
        )

    );
  }
}

class Profilemodel{
  final String name;
  final int age;
  Profilemodel({this.name , this.age});
}

2 Answers 2

6

I guess that you have not overriden the == operator (and consequently hashCode) in your Profilemodel class. Hence items.toSet().toList() might not work as you expected.

If you need a very simple solution, you can try something like this,

List<People> items = [
  Profilemodel( age: 20 , name: 'Ahmed' ) ,
  Profilemodel( age: 30 , name: 'Fatma' ),
  Profilemodel( age: 15 , name: 'Ahmed' ),
  Profilemodel( age: 15 , name: 'jon' ),
  Profilemodel( age: 25 , name: 'Fatma' )
];
final Map<String, People> profileMap = new Map();
items.forEach((item) {
  profileMap[item.name] = item;
});
items = profileMap.values.toList();

Or you can always try to override what I mentioned in the above. Read this article for more information on it.

Sign up to request clarification or add additional context in comments.

Comments

2

There are many ways you can try to remove duplicates items from a list. for example you can convert your list to a set collection.

Set<Profilemodel> set = new Set<Profilemodel>.from(items );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.