skip to Main Content

I am making some entries and now i want to fetch recent entries based on a field

Like i have list of available Games.. and now i want to fatch recent 3 unique games from last entries..and want to show this three games in top of list and all other remaining games in sorted a->z order….

here is my code

void main() {
  List<String> allGames = [
    'Football',
    'Cricket',
    'Tennis',
    'Hockey',
    'Baseball',
    'Race',
    'Swimming',
    'Polo',
    'Rugby',
    'Puzzle',
    'Soccer',
  ];
  List<String> recentGames = [
    'Football',
    'Football',
    'Cricket',
    'Football',
    'Soccer',
    'Football',
    'Football',
    'Soccer',
    'Cricket',
    'Puzzle',
    'Puzzle'
  ];

  //1  )first of all i want only first 3 unique Games from recentGames
  // List<String> recent3= Football  Cricket Soccer

  //2  ) now want to merge all remaining games with recent3 games
  //like remainingGames=allGames-recent3 ,sorted in A-> Z
  // remainingGames= BaseBall Hockey Polo Race Rugby Swimming Tennis

  //3) merge recent3+ remainingGames
  // resultGames=Football Cricket Soccer Baseball Hockey Polo Race Rugby Swimming Tennis;

  List<String> resultGames = [];

  // logic code

  print(resultGames);
}

2

Answers


  1. It looks like you’re trying to achieve the following steps:

    1. Get the first 3 unique games from the recentGames list.
    2. Find the remaining games by subtracting the recent games from the allGames list and sort them alphabetically.
    3. Merge the recent games with the remaining games.

    Here’s the code to achieve this:

    void main() {
      List<String> allGames = [
        'Football',
        'Cricket',
        'Tennis',
        'Hockey',
        'Baseball',
        'Race',
        'Swimming',
        'Polo',
        'Rugby',
        'Puzzle',
        'Soccer',
      ];
      List<String> recentGames = [
        'Football',
        'Football',
        'Cricket',
        'Football',
        'Soccer',
        'Football',
        'Football',
        'Soccer',
        'Cricket',
        'Puzzle',
        'Puzzle',
      ];
    
      // Step 1: Get the first 3 unique games from recentGames
      Set<String> uniqueRecentGames = {};
      List<String> recent3 = [];
      for (String game in recentGames) {
        if (!uniqueRecentGames.contains(game)) {
          uniqueRecentGames.add(game);
          recent3.add(game);
          if (recent3.length >= 3) {
            break;
          }
        }
      }
    
      // Step 2: Find remaining games and sort them alphabetically
      List<String> remainingGames = List.from(allGames);
      remainingGames.removeWhere((game) => recent3.contains(game));
      remainingGames.sort();
    
      // Step 3: Merge recent3 and remainingGames
      List<String> resultGames = [...recent3, ...remainingGames];
    
      print(resultGames);
    }
    

    This code will give you the desired output where the first 3 unique recent games are followed by the remaining games in alphabetical order.

    Login or Signup to reply.
    1. To get the first 3 unique games from recentGames
        ///
        /// `toSet()` to make sure the List has no duplicate value
        ///
        List<String> recent3 = recentGames.toSet().take(3).toList(); 
    
    1. To find the remaining games and sort them alphabetically
        ///
        /// `sort()` by default in ASC alphabetical order
        ///
        List<String> remainingGames = allGames..addAll(recent3)
                                              ..sort()
                                              ..toList();
    
    

    3

        ///
        /// `...` spread operator to expand iterable data to list
        ///
        List<String> resultGames =  [...recent3, ...remainingGames].toSet().toList();
    
    • Output (p.s. I use logger package to print this output)
    flutter: ^[[38;5;12m│ 💡 [<…>
    flutter: ^[[38;5;12m│ 💡   "Football",<…>
    flutter: ^[[38;5;12m│ 💡   "Cricket",<…>
    flutter: ^[[38;5;12m│ 💡   "Soccer",<…>
    flutter: ^[[38;5;12m│ 💡   "Baseball",<…>
    flutter: ^[[38;5;12m│ 💡   "Hockey",<…>
    flutter: ^[[38;5;12m│ 💡   "Polo",<…>
    flutter: ^[[38;5;12m│ 💡   "Puzzle",<…>
    flutter: ^[[38;5;12m│ 💡   "Race",<…>
    flutter: ^[[38;5;12m│ 💡   "Rugby",<…>
    flutter: ^[[38;5;12m│ 💡   "Swimming",<…>
    flutter: ^[[38;5;12m│ 💡   "Tennis"<…>
    flutter: ^[[38;5;12m│ 💡 ]<…>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search