I have a function as follows. All my GamePlayers are getting updated with the counter value of 1 - because the counter variable is updated after all update statements have completed.
Why is this like that and what can i do to make the counter increment each time a update query is done?
Future<bool> closeGame(List<GamePlayer> sortedWinnerList, Game game) async {
try {
int counter = 1;
await _database.transaction((txn) async {
sortedWinnerList.forEach((element) async {
print('xx update gamePlayer, counter= $counter');
await txn.execute(
'update $tableName_GAMEPLAYER set result_position = $counter where id=${element.id}');
GameResult newResult = new GameResult(
gameId: game.id,
playerId: element.playerId,
amount: element.gameWinAmount);
await txn.insert(tableName_GAMERESULT, newResult.toJson());
print('xx loop end, counter= $counter');
counter++;
});
await txn.execute(
'update $tableName_GAME set ended = ${DateTime.now().millisecondsSinceEpoch}');
});
return true;
} catch (e) {
print('Game not closed: $e');
}
return false;
}