Rework data structure to map from list

This commit is contained in:
2025-02-22 15:31:23 +01:00
parent c94a8f8926
commit fef5f199c3
2 changed files with 25 additions and 21 deletions

View File

@@ -53,7 +53,7 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
late List<Game> games = [];
late Map<String, Game> games = {};
@override
void initState() {
@@ -86,31 +86,33 @@ class _MyHomePageState extends State<MyHomePage> {
spacing: 4,
runSpacing: 4,
children: [
...games.map((game) => SizedBox(
width: 400,
child: GameCard(
game: game,
onGameUpdated: (game) async {
game = await GameRepository.upsert(game);
setState(() {
games[games.indexOf(game)] = game;
});
},
onDelete: () async {
await GameRepository.delete(game);
setState(() {
games.remove(game);
});
},
...games.values.map(
(game) => SizedBox(
width: 400,
child: GameCard(
game: game,
onGameUpdated: (game) async {
game = await GameRepository.upsert(game);
setState(() {
games[game.name] = game;
});
},
onDelete: () async {
await GameRepository.delete(game);
setState(() {
games.remove(game.name);
});
},
),
),
)),
),
SizedBox(
width: 400,
child: NewGameCard(
onGameCreated: (game) async {
game = await GameRepository.upsert(game);
setState(() {
games.add(game);
games[game.name] = game;
});
},
),