diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index c2e9d72..063ac32 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -29,7 +29,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { } final players = await db.playerMatchDao.getPlayersOfMatch( matchId: row.id, - ); + ) ?? []; return Match( id: row.id, name: row.name ?? '', @@ -56,10 +56,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { group = await db.groupDao.getGroupById(groupId: result.groupId!); } - List? players; - if (await db.playerMatchDao.matchHasPlayers(matchId: matchId)) { - players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId); - } + final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; return Match( id: result.id, @@ -91,13 +88,11 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { mode: InsertMode.insertOrReplace, ); - if (match.players != null) { - for (final p in match.players!) { - await db.playerMatchDao.addPlayerToMatch( - matchId: match.id, - playerId: p.id, - ); - } + for (final p in match.players) { + await db.playerMatchDao.addPlayerToMatch( + matchId: match.id, + playerId: p.id, + ); } }); } @@ -180,10 +175,8 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { // Add all players of the matches in batch (unique) final uniquePlayers = {}; for (final match in matches) { - if (match.players != null) { - for (final p in match.players!) { - uniquePlayers[p.id] = p; - } + for (final p in match.players) { + uniquePlayers[p.id] = p; } // Also include members of groups if (match.group != null) { @@ -215,18 +208,16 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { // Add all player-match associations in batch await db.batch((b) { for (final match in matches) { - if (match.players != null) { - for (final p in match.players!) { - b.insert( - db.playerMatchTable, - PlayerMatchTableCompanion.insert( - matchId: match.id, - playerId: p.id, - score: 0, - ), - mode: InsertMode.insertOrIgnore, - ); - } + for (final p in match.players) { + b.insert( + db.playerMatchTable, + PlayerMatchTableCompanion.insert( + matchId: match.id, + playerId: p.id, + score: 0, + ), + mode: InsertMode.insertOrIgnore, + ); } } }); @@ -372,15 +363,15 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { /// TEMPORARY: Checks if a match has a winner. /// Currently returns true if the match has any players. Future hasWinner({required String matchId}) async { - final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId); - return players?.isNotEmpty ?? false; + final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; + return players.isNotEmpty; } /// TEMPORARY: Gets the winner of a match. /// Currently returns the first player in the match's player list. Future getWinner({required String matchId}) async { - final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId); - return (players?.isNotEmpty ?? false) ? players!.first : null; + final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; + return players.isNotEmpty ? players.first : null; } /// TEMPORARY: Sets the winner of a match. diff --git a/lib/data/dto/match.dart b/lib/data/dto/match.dart index 559914e..31f45fc 100644 --- a/lib/data/dto/match.dart +++ b/lib/data/dto/match.dart @@ -12,7 +12,7 @@ class Match { final String name; final Game game; final Group? group; - final List? players; + final List players; final String notes; Player? winner; @@ -23,7 +23,7 @@ class Match { required this.name, required this.game, this.group, - this.players, + this.players = const [], required this.notes, this.winner, }) : id = id ?? const Uuid().v4(), @@ -54,7 +54,7 @@ class Match { 'name': name, 'gameId': game.id, 'groupId': group?.id, - 'playerIds': (players ?? []).map((player) => player.id).toList(), + 'playerIds': players.map((player) => player.id).toList(), 'notes': notes, }; } diff --git a/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart b/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart index 163ebd2..7096702 100644 --- a/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart +++ b/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart @@ -63,7 +63,7 @@ class _CreateMatchViewState extends State { int selectedGameIndex = -1; /// The currently selected players - List? selectedPlayers; + List selectedPlayers = []; @override void initState() { @@ -178,7 +178,7 @@ class _CreateMatchViewState extends State { Expanded( child: PlayerSelection( key: ValueKey(selectedGroup?.id ?? 'no_group'), - initialSelectedPlayers: selectedPlayers ?? [], + initialSelectedPlayers: selectedPlayers, availablePlayers: filteredPlayerList, onChanged: (value) { setState(() { @@ -259,6 +259,6 @@ class _CreateMatchViewState extends State { /// - Either a group is selected OR at least 2 players are selected bool _enableCreateGameButton() { return (selectedGroup != null || - (selectedPlayers != null && selectedPlayers!.length > 1)); + (selectedPlayers.length > 1)); } } \ No newline at end of file diff --git a/lib/presentation/views/main_menu/match_view/match_result_view.dart b/lib/presentation/views/main_menu/match_view/match_result_view.dart index 75015f0..981a196 100644 --- a/lib/presentation/views/main_menu/match_view/match_result_view.dart +++ b/lib/presentation/views/main_menu/match_view/match_result_view.dart @@ -153,12 +153,10 @@ class _MatchResultViewState extends State { List getAllPlayers(Match match) { List players = []; - if (match.group == null && match.players != null) { - players = [...match.players!]; - } else if (match.group != null && match.players != null) { - players = [...match.players!, ...match.group!.members]; + if (match.group == null) { + players = [...match.players]; } else { - players = [...match.group!.members]; + players = [...match.players, ...match.group!.members]; } players.sort((a, b) => a.name.compareTo(b.name)); diff --git a/lib/presentation/views/main_menu/statistics_view.dart b/lib/presentation/views/main_menu/statistics_view.dart index af3eba6..5b3ff22 100644 --- a/lib/presentation/views/main_menu/statistics_view.dart +++ b/lib/presentation/views/main_menu/statistics_view.dart @@ -202,19 +202,17 @@ class _StatisticsViewState extends State { } } } - if (match.players != null) { - final members = match.players!.map((p) => p.id).toList(); - for (var playerId in members) { - final index = matchCounts.indexWhere((entry) => entry.$1 == playerId); - // -1 means player not found in matchCounts - if (index != -1) { - final current = matchCounts[index].$2; + final members = match.players.map((p) => p.id).toList(); + for (var playerId in members) { + final index = matchCounts.indexWhere((entry) => entry.$1 == playerId); + // -1 means player not found in matchCounts + if (index != -1) { + final current = matchCounts[index].$2; matchCounts[index] = (playerId, current + 1); } else { matchCounts.add((playerId, 1)); } } - } } // Adding all players with zero matches diff --git a/lib/presentation/widgets/tiles/match_tile.dart b/lib/presentation/widgets/tiles/match_tile.dart index ab65e5d..7862000 100644 --- a/lib/presentation/widgets/tiles/match_tile.dart +++ b/lib/presentation/widgets/tiles/match_tile.dart @@ -91,7 +91,7 @@ class _MatchTileState extends State { const SizedBox(width: 6), Expanded( child: Text( - '${group.name}${widget.match.players != null ? ' + ${widget.match.players?.length}' : ''}', + '${group.name} + ${widget.match.players.length}', style: const TextStyle(fontSize: 14, color: Colors.grey), overflow: TextOverflow.ellipsis, ), @@ -106,7 +106,7 @@ class _MatchTileState extends State { const SizedBox(width: 6), Expanded( child: Text( - '${widget.match.players!.length} ${loc.players}', + '${widget.match.players.length} ${loc.players}', style: const TextStyle(fontSize: 14, color: Colors.grey), overflow: TextOverflow.ellipsis, ), @@ -241,12 +241,10 @@ class _MatchTileState extends State { final playerIds = {}; // Add players from game.players - if (widget.match.players != null) { - for (var player in widget.match.players!) { - if (!playerIds.contains(player.id)) { - allPlayers.add(player); - playerIds.add(player.id); - } + for (var player in widget.match.players) { + if (!playerIds.contains(player.id)) { + allPlayers.add(player); + playerIds.add(player.id); } } diff --git a/lib/services/data_transfer_service.dart b/lib/services/data_transfer_service.dart index 4995c77..526a459 100644 --- a/lib/services/data_transfer_service.dart +++ b/lib/services/data_transfer_service.dart @@ -64,7 +64,7 @@ class DataTransferService { 'endedAt': m.endedAt?.toIso8601String(), 'gameId': m.game.id, 'groupId': m.group?.id, - 'playerIds': (m.players ?? []).map((p) => p.id).toList(), + 'playerIds': m.players.map((p) => p.id).toList(), 'notes': m.notes, }) .toList(), @@ -210,7 +210,7 @@ class DataTransferService { name: map['name'] as String, game: game ?? Game(name: 'Unknown', ruleset: Ruleset.singleWinner, description: '', color: GameColor.blue, icon: ''), group: group, - players: players.isNotEmpty ? players : null, + players: players, createdAt: DateTime.parse(map['createdAt'] as String), endedAt: endedAt, notes: map['notes'] as String? ?? '',