首页 > 解决方案 > 如何删除列表的重复项(飞镖/颤振)

问题描述

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

这是我的自定义模型:

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

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

  static List<HomeList> homeList = [];
}

这是我从 cloud_firestore 获取数据的 futureBuilder 函数:

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

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

标签: listflutterdart

解决方案


要删除重复项,您可以使用 Set Data Structure 而不是 List。

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


推荐阅读