如何删除 List<MyDataModel> 的重复项(Dart/Flutter)

All*_*nzo 2 list dart flutter

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

这是我的定制模型:

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

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

  static List<HomeList> homeList = [];
}

Run Code Online (Sandbox Code Playgroud)

这是我从 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;
  }
Run Code Online (Sandbox Code Playgroud)

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

Jay*_*ara 6

要删除重复项,您可以使用设置数据结构而不是列表。

只需使用 Set 而不是 List 即可仅获取唯一值。