0

複数のフィールドでグループ化し、各学生の最小値を取得する必要があります。私はダーツが初めてなので、実装方法がわかりません。以下は、リアルタイムの firebase テーブル構造の例です。

"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",
    }
  },

予想される出力は次のとおりです。

Puzzle John Doe  99
Puzzle Mary Doe  87 
4

2 に答える 2

0

明らかに「宿題をする」ような質問なので、質問には完全には答えません。

しかし、プログラミングの開始を支援するために、ここに「ほぼ」全体のソリューションを投稿すると役立つと思いました。最終的な解決策にたどり着くために、たとえばthis oneなど、他の関連する質問の回答を読んでいただければ幸いです。

ほら、これで問題の「配管」部分が解決します。

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() ?? "?";
}

これを実行すると、次のように出力されます。

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

フクロウの残りを仕上げて頑張ってください:)

于 2021-10-26T14:49:05.207 に答える