0

I am new to dart, I have a requirement to group by multiple fields and get the minimum value of each student. I don't have an idea on how to implement since I am new in dart. Below is the example of the realtime firebase table structure.

"gameRanking" : {
    "-MmvcDgrGsuCjhcsmXfP" : {
      "game" : "Puzzle",
      "score" : "105",
      "student" : "John Doe",   
    },
    "-MasdDgrGsuCjhcsmXfP" : {
      "game" : "Puzzle",
      "score" : "99",
      "student" : "John Doe",      
    },
    "-Mmw0kagqLrEbdWlkXg7" : {
      "game" : "Puzzle",
      "score" : "87",
      "student" : "Mary Doe",
    },
    "-MmwC8ONbJUWzP_Wa7X0" : {
      "game" : "Puzzle",
      "score" : "95",
      "student" : "Mary Doe",
    }
  },

Here is the expected output:

Puzzle John Doe  99
Puzzle Mary Doe  87 
cris gomez
  • 131
  • 1
  • 15
  • Hey, welcome to SO. To make it easier to help you, please include code you've tried already, even if it doesn't work (just point out where it's not working). That will make it much easier to help. – Renato Oct 26 '21 at 14:27
  • If you've got no idea at all, look at this answer first: https://stackoverflow.com/questions/54029370/flutter-dart-how-to-groupby-list-of-maps – Renato Oct 26 '21 at 14:34
  • Hello @Renato, I don't have a code yet, since I am in the process of exploring dart and flutter. – cris gomez Oct 26 '21 at 14:34
  • Ok, but then you should probably put some effort into learning the basics first... learn how `Map`, specifically, works in Dart... then you should be able to ask more meaningful questions. – Renato Oct 26 '21 at 14:36
  • I already take a look the link you've mentioned, but it is not fit on my needs. – cris gomez Oct 26 '21 at 14:36
  • You're mistaken. It's definitely what you need. If you can't see why, you're missing basic understanding without which you'll only be able to copy/paste the solution someone might post for you here without actually understanding it. – Renato Oct 26 '21 at 14:38

2 Answers2

0

I won't answer the question entirely because it's clearly a "do my homework" kind of question.

But in order to assist you in getting started with programming, I thought it would be useful to post "nearly" the whole solution here. I hope you will be able to then read answers in other related questions, for example this one, to get to the final solution.

Here you go, this solves the "plumbing" part of the problem:

class Stats {
  String game;
  int score;
  String student;
  Stats(this.game, this.score, this.student);
  @override
  String toString() => "Stats($game, $score, $student)";
}

main() {
  final map = {
    "gameRanking": {
      "-MmvcDgrGsuCjhcsmXfP": {
        "game": "Puzzle",
        "score": "105",
        "student": "John Doe",
      },
      "-MasdDgrGsuCjhcsmXfP": {
        "game": "Puzzle",
        "score": "99",
        "student": "John Doe",
      },
      "-Mmw0kagqLrEbdWlkXg7": {
        "game": "Puzzle",
        "score": "87",
        "student": "Mary Doe",
      },
      "-MmwC8ONbJUWzP_Wa7X0": {
        "game": "Puzzle",
        "score": "95",
        "student": "Mary Doe",
      }
    },
  };

  final rankingsById = map["gameRanking"] ?? {};

  final rankings = rankingsById.map((id, data) {
    return MapEntry(
        id,
        Stats(data["game"].coerceToString(), data["score"].coerceToInt(),
            data["student"].coerceToString()));
  }).values;

  rankings.forEach((stats) => print(stats));
}

extension on Object? {
  int coerceToInt() => int.parse(coerceToString());

  String coerceToString() => this?.toString() ?? "?";
}

Running this prints:

Stats(Puzzle, 105, John Doe)
Stats(Puzzle, 99, John Doe)
Stats(Puzzle, 87, Mary Doe)
Stats(Puzzle, 95, Mary Doe)

Good luck finishing the rest of the owl :)

Renato
  • 12,940
  • 3
  • 54
  • 85
0

I needed the same... I don't know if this is the best solution...

Map<int,List<MyObject>> mom=groupBy(_myObjectList,(element)=>element.cornerCnt);//get a Map with cornerCnt as key and a List of myObjects for every key

    int maxCornerCnt;

    int max=0;

    mom.forEach((key, moList) { //for every map entry
      int len=moList.length;    //get lenght of myObject-list
      if (len>max) {
        max=len;
        maxCornerCnt=key;       //write key(cornerCnt) to  maxCornerCnt 
      }
    });
MCB
  • 503
  • 1
  • 8
  • 21