且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查列表中是否已经存在字符串?

更新时间:2021-11-23 19:01:40

仅循环遍历从firebase中获得的主题标签,然后仅将其添加到结果中(如果它们还没有在其中):

Just loop over the hashtags you get from firebase and add them to your results only if they are not already in there like this:

Future<List<String>> getHashtags() async {
  List<String> allVideoHastags = [];

  QuerySnapshot snapshots =
      await FirebaseFirestore.instance.collection('videos').get();

  for (QueryDocumentSnapshot videoSnapshot in snapshots.docs) {
    List<String> videoHastags =
        List.from(videoSnapshot.data()['Hashtagsforallvideos']);

    videoHastags.forEach((element) {
      if(!allVideoHastags.contains(element)){
          allVideoHastags.add(element);
      }
    });
    
  }
  _allResults = allVideoHastags;
  searchResults();

  return allVideoHastags;
}