Flutter Tinder 之类的应用程序性能问题

问题描述 投票:0回答:1

Stack Overflow 社区!

我正在为移动应用程序开发一项功能,用户可以在其中滑动浏览项目列表(例如,以书籍为例),并且该应用程序将根据一组用户偏好(例如,首选)来匹配这些项目流派)。每个项目都有自己的特征列表(在本例中为流派),我需要将这些特征与用户的偏好进行比较以找到匹配项。

我当前在 Dart 中实现的核心看起来像这样:

void checkForMatch() {
 if (preferredGenres != null) {
var preferredGenresKeys = HashSet<String>.from(
  preferredGenres!.map((genre) => genre.genreKey)
);

remainingBooks?.removeWhere((book) {
  bool allGenresMatch = true;
  for (var genreKey in book.genresForBook) {
    if (!preferredGenresKeys.contains(genreKey)) {
      allGenresMatch = false;
      break;
    }
  }

  if (allGenresMatch) {
    matchedBooksSet.add(book);
  }

  return allGenresMatch;
});

} }

每次用户滑动时都会调用此方法,但我遇到了性能问题,尤其是当项目和首选项列表变大时。延迟变得明显,影响用户体验。

任何见解、代码片段或相关资源的指针将不胜感激。预先感谢您的帮助!

flutter performance dart swipe
1个回答
0
投票

您可以像这样使用 checkForMatch() 函数。

void checkForMatch() {
      // Check if preferredGenres is not null
      if (preferredGenres != null) {
        // Create a set of genre keys from preferredGenres
        var preferredGenresKeys = HashSet<String>.from(
            preferredGenres!.map((genre) => genre.genreKey));
    
        // Initialize a list to store matched books
        List<Book> matchedBooks = [];
    
        // Iterate through remainingBooks
        for (var book in remainingBooks!) {
          // Assume all genres match initially
          bool allGenresMatch = true;
    
          // Check if all genres of the book are present in preferredGenresKeys
          for (var genreKey in book.genresForBook) {
            if (!preferredGenresKeys.contains(genreKey)) {
              // If a genre doesn't match, set allGenresMatch to false and break the loop
              allGenresMatch = false;
              break;
            }
          }
          
          // If all genres match, add the book to matchedBooks
          if (allGenresMatch) {
            matchedBooks.add(book);
          }
        }
        
        // Add all matched books to matchedBooksSet
        matchedBooksSet.addAll(matchedBooks);
        
        // Remove matched books from remainingBooks
        remainingBooks!.removeWhere((book) => matchedBooks.contains(book));
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.