Это работает ...
final List<Player> playersList = [
Player(playerName: 'player1', playerValue: 20),
Player(playerName: 'player2', playerValue: 20),
Player(playerName: 'player1', playerValue: 30),
Player(playerName: 'player3', playerValue: 50),
Player(playerName: 'player5', playerValue: 60),
];
playersList.shuffle();
var player1 = playersList.first;
var player2 = player1;
while (player1.playerName == player2.playerName) {
playersList.shuffle();
player2 = playersList.first;
}
print(player1);
print(player2);
ОБНОВЛЕНИЕ: Оберните его в метод класса Player
class Player {
final String playerName;
final int playerValue;
Player({this.playerName, this.playerValue});
static List<Player> pairPlayers() {
final List<Player> playersList = [
Player(playerName: 'player1', playerValue: 20),
Player(playerName: 'player2', playerValue: 20),
Player(playerName: 'player1', playerValue: 30),
Player(playerName: 'player3', playerValue: 50),
Player(playerName: 'player5', playerValue: 60),
];
playersList.shuffle();
var player1 = playersList.first;
var player2 = player1;
while (player1.playerName == player2.playerName) {
playersList.shuffle();
player2 = playersList.first;
}
return [player1, player2];
}
@override
String toString() {
return playerName;
}
}
Затем в любое время, когда вы хотите объединить игроков, используйте
Players.pairPlayers();