From 7810443a002ac178ba6acecf1702d0d02041337b Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Sat, 7 Mar 2026 22:11:51 +0100 Subject: [PATCH] Fix: SetState Error --- .../widgets/player_selection.dart | 88 ++++++++++--------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/lib/presentation/widgets/player_selection.dart b/lib/presentation/widgets/player_selection.dart index cc3a68c..b51cadc 100644 --- a/lib/presentation/widgets/player_selection.dart +++ b/lib/presentation/widgets/player_selection.dart @@ -110,7 +110,9 @@ class _PlayerSelectionState extends State { final bool nameMatches = player.name.toLowerCase().contains( value.toLowerCase(), ); - final bool isNotSelected = !selectedPlayers.any((p) => p.id == player.id); + final bool isNotSelected = !selectedPlayers.any( + (p) => p.id == player.id, + ); return nameMatches && isNotSelected; }).toList(); } @@ -224,49 +226,53 @@ class _PlayerSelectionState extends State { db.playerDao.getAllPlayers(), Future.delayed(Constants.MINIMUM_SKELETON_DURATION), ]).then((results) => results[0] as List); - if (mounted) { - _allPlayersFuture.then((loadedPlayers) { - setState(() { - // If a list of available players is provided (even if empty), use that list. - if (widget.availablePlayers != null) { - widget.availablePlayers!.sort((a, b) => a.name.compareTo(b.name)); - allPlayers = [...widget.availablePlayers!]; - suggestedPlayers = [...allPlayers]; - if (widget.initialSelectedPlayers != null) { - // Ensures that only players available for selection are pre-selected. - selectedPlayers = widget.initialSelectedPlayers! - .where( - (p) => widget.availablePlayers!.any( - (available) => available.id == p.id, - ), - ) - .toList(); - } - } else { - // Otherwise, use the loaded players from the database. - loadedPlayers.sort((a, b) => a.name.compareTo(b.name)); - allPlayers = [...loadedPlayers]; - if (widget.initialSelectedPlayers != null) { - // Excludes already selected players from the suggested players list. - suggestedPlayers = loadedPlayers.where((p) => !widget.initialSelectedPlayers!.any((ip) => ip.id == p.id)).toList(); - // Ensures that only players available for selection are pre-selected. - selectedPlayers = widget.initialSelectedPlayers! - .where( - (p) => allPlayers.any( - (available) => available.id == p.id, - ), - ) - .toList(); - } else { - // If no initial selection, all loaded players are suggested. - suggestedPlayers = [...loadedPlayers]; - } + _allPlayersFuture.then((loadedPlayers) { + if (!mounted) return; + setState(() { + // If a list of available players is provided (even if empty), use that list. + if (widget.availablePlayers != null) { + widget.availablePlayers!.sort((a, b) => a.name.compareTo(b.name)); + allPlayers = [...widget.availablePlayers!]; + suggestedPlayers = [...allPlayers]; + + if (widget.initialSelectedPlayers != null) { + // Ensures that only players available for selection are pre-selected. + selectedPlayers = widget.initialSelectedPlayers! + .where( + (p) => widget.availablePlayers!.any( + (available) => available.id == p.id, + ), + ) + .toList(); } - isLoading = false; - }); + } else { + // Otherwise, use the loaded players from the database. + loadedPlayers.sort((a, b) => a.name.compareTo(b.name)); + allPlayers = [...loadedPlayers]; + if (widget.initialSelectedPlayers != null) { + // Excludes already selected players from the suggested players list. + suggestedPlayers = loadedPlayers + .where( + (p) => !widget.initialSelectedPlayers!.any( + (ip) => ip.id == p.id, + ), + ) + .toList(); + // Ensures that only players available for selection are pre-selected. + selectedPlayers = widget.initialSelectedPlayers! + .where( + (p) => allPlayers.any((available) => available.id == p.id), + ) + .toList(); + } else { + // If no initial selection, all loaded players are suggested. + suggestedPlayers = [...loadedPlayers]; + } + } + isLoading = false; }); - } + }); } /// Adds a new player to the database from the search bar input.