List 如何删除列表的重复项<;MyDataModel>;(飞镖/颤振)

List 如何删除列表的重复项<;MyDataModel>;(飞镖/颤振),list,flutter,dart,List,Flutter,Dart,我有一个futurebuilder,它基于一个列表构建UI,它完成了这项工作,但是由于每次导航时都会一次又一次地构建UI,因此我会得到重复的UI。我的问题是,Dart中是否有一种天生的方法可以从列表中删除重复项?我已经试过了,但是不起作用 这是我的定制模型: class HomeList { Widget navigateScreen; String imagePath; PatientInfo patientInfo; HomeList({ this.navigate

我有一个futurebuilder,它基于一个列表构建UI,它完成了这项工作,但是由于每次导航时都会一次又一次地构建UI,因此我会得到重复的UI。我的问题是,Dart中是否有一种天生的方法可以从列表中删除重复项?我已经试过了,但是不起作用

这是我的定制模型:

class HomeList {
  Widget navigateScreen;
  String imagePath;
  PatientInfo patientInfo;

  HomeList({
    this.navigateScreen,
    this.imagePath = '',
    this.patientInfo,
  });

  static List<HomeList> homeList = [];
}

类主页列表{
小部件导航屏幕;
字符串图像路径;
PatientInfo PatientInfo;
家庭名单({
这是导航屏幕,
this.imagePath=“”,
这是patientInfo,
});
静态列表homeList=[];
}
以下是我的futureBuilder功能,我正在从我的cloud\u firestore获取数据:

  _getPatients() async {
    if (didLoadpatients == 0) {
      print('this is didloadpatients at start of func $didLoadpatients');
      var document = await db
          .collection('users')
          .document(mUser.uid)
          .collection('patients');

      document.getDocuments().then((QuerySnapshot query) async {
        query.documents.forEach((f) {
          uids.add(f.data['uID']);
        });
        didLoadpatients++;
      print('this is didloadpatients at end of func $didLoadpatients');
        for (var i = 0; i < uids.length; i++) {
          var userDocuments = await db.collection('users').document(uids[i]);
          userDocuments.get().then((DocumentSnapshot doc) {
            print(doc.data);
            homeList.add(HomeList(
                imagePath: 'assets/fitness_app/fitness_app.png',
                patientInfo: new PatientInfo.fromFbase(doc.data)));
          });
          print(homeList);
        }
      });
    } else 
    print('I am leaving the get patient function');
  }

  Future<bool> getData() async {
    _getCurrentUser();
    await Future.delayed(const Duration(milliseconds: 1500), () async {
      _getPatients();
    });
    return true;
  }
\u getPatients()异步{
if(didloadparticles==0){
打印(“这是func$DIDLOADPAINTS开头的DIDLOADPAINTS”);
var document=await db
.collection('用户')
.文件(mUser.uid)
.收集(“患者”);
document.getDocuments().then((QuerySnapshot查询)异步{
query.documents.forEach((f){
添加(f.data['uID']);
});
did++;
打印(“这是func$didloadpatients结尾处的didloadpatients”);
对于(变量i=0;i

任何帮助都将不胜感激,谢谢

要删除重复项,可以使用集合数据结构而不是列表


只需使用Set而不是List来获取唯一的值。

我想出了一个相当强大的解决方案。而不是

_getPatients() async {
    if (didLoadpatients == 0) {
      print('this is didloadpatients at start of func $didLoadpatients');
      var document = await db
          .collection('users')
          .document(mUser.uid)
          .collection('patients');

      document.getDocuments().then((QuerySnapshot query) async {
        query.documents.forEach((f) {
          uids.add(f.data['uID']);
        });
        didLoadpatients++;
      print('this is didloadpatients at end of func $didLoadpatients');
        for (var i = 0; i < uids.length; i++) {
          var userDocuments = await db.collection('users').document(uids[i]);
          userDocuments.get().then((DocumentSnapshot doc) {
            print(doc.data);
            homeList.add(HomeList(
                imagePath: 'assets/fitness_app/fitness_app.png',
                patientInfo: new PatientInfo.fromFbase(doc.data)));
          });
          print(homeList);
        }
      });
    } else 
    print('I am leaving the get patient function');
  }
\u getPatients()异步{
if(didloadparticles==0){
打印(“这是func$DIDLOADPAINTS开头的DIDLOADPAINTS”);
var document=await db
.collection('用户')
.文件(mUser.uid)
.收集(“患者”);
document.getDocuments().then((QuerySnapshot查询)异步{
query.documents.forEach((f){
添加(f.data['uID']);
});
did++;
打印(“这是func$didloadpatients结尾处的didloadpatients”);
对于(变量i=0;i
我已经按照@Jay Mungara所说的做了,每次我的UI重建时都会清除我的设置:

_getPatients() async {
homeList.clear();
    if (didLoadpatients == 0) {
      print('this is didloadpatients at start of func $didLoadpatients');
      var document = await db
          .collection('users')
          .document(mUser.uid)
          .collection('patients');

      document.getDocuments().then((QuerySnapshot query) async {
        query.documents.forEach((f) {
          uids.add(f.data['uID']);
        });
        didLoadpatients++;
      print('this is didloadpatients at end of func $didLoadpatients');
        for (var i = 0; i < uids.length; i++) {
          var userDocuments = await db.collection('users').document(uids[i]);
          userDocuments.get().then((DocumentSnapshot doc) {
            print(doc.data);
            homeList.add(HomeList(
                imagePath: 'assets/fitness_app/fitness_app.png',
                patientInfo: new PatientInfo.fromFbase(doc.data)));
          });
          print(homeList);
        }
      });
    } else 
    print('I am leaving the get patient function');
  }
\u getPatients()异步{
homeList.clear();
if(didloadparticles==0){
打印(“这是func$DIDLOADPAINTS开头的DIDLOADPAINTS”);
var document=await db
.collection('用户')
.文件(mUser.uid)
.收集(“患者”);
document.getDocuments().then((QuerySnapshot查询)异步{
query.documents.forEach((f){
添加(f.data['uID']);
});
did++;
打印(“这是func$didloadpatients结尾处的didloadpatients”);
对于(变量i=0;i

谢谢你的回答

要从自定义对象列表中删除重复的元素,您需要在POJO类中重写==和hashcode方法,然后在集合中添加项,然后再次将集合转换为列表以删除重复的对象。以下是工作代码:-

class TrackPointList {
double latitude;
double longitude;
String eventName;
Time timeZone;

TrackPointList({
this.latitude,
this.longitude,
this.eventName,
this.timeZone,
}))

}

}

factory TrackPointList.fromJson(映射json)=>TrackPointList(
纬度:json[“纬度”].toDouble(),
经度:json[“经度”].toDouble(),
eventName:json[“eventName”],
时区:timeValues.map[json[“timeZone”]],
))

Map to json()=>{
“纬度”:纬度,
“经度”:经度,
“eventName”:eventName,
“时区”:时间值。反转[时区],
}); }

上面是POJO类。下面是帮助您根据eventName数据成员筛选对象的方法

List<TrackPointList> getFilteredList(List<TrackPointList> list){

final existing = Set<TrackPointList>();
final unique = list
    .where((trackingPoint) => existing.add(trackingPoint))
    .toList();

return unique;
List getFilteredList(列表){
最终存在=集合();
最终唯一=列表
.其中((trackingPoint)=>existing.add(trackingPoint))
.toList();
返回唯一值;
}

这肯定会奏效的。
如果有帮助,请+1

列表包含哪些元素?列表包含1。图像资源的路径,2。另一个自定义模型包含用户信息,最后是一个小部件,如果用户按下Image.asset,他/她将被导航到该屏幕。我是否实现了错误?每当我加载带有FutureBuilder的屏幕时,它仍然会重复。。我更改了
静态列表=[]到静态设置myVariable=new LinkedHashSet();以及代码中从列表到集合的所有其他数据类型,但它仍然包含重复的数据类型
int _hashCode;
@override
int get hashCode {
if(_hashCode == null) {
  _hashCode = eventName.hashCode;
}
return _hashCode;
factory TrackPointList.fromJson(Map<String, dynamic> json) => TrackPointList(
latitude: json["latitude"].toDouble(),
longitude: json["longitude"].toDouble(),
eventName: json["eventName"],
timeZone: timeValues.map[json["timeZone"]],
Map<String, dynamic> toJson() => {
"latitude": latitude,
"longitude": longitude,
"eventName": eventName,
"timeZone": timeValues.reverse[timeZone],
List<TrackPointList> getFilteredList(List<TrackPointList> list){

final existing = Set<TrackPointList>();
final unique = list
    .where((trackingPoint) => existing.add(trackingPoint))
    .toList();

return unique;