I have issues retrieving data from a db in Room.
Inside the navHost I get a list of all the elements from one table of the database (players of a team) and then pass the list to a composable containing a LazyColumn and a list ->
composable(route = ClappsicScreen.Roster.name) {
val players by viewModel.getPlayers().collectAsState(emptyList())
/* Add Roster Page here */
TeamScreen(
players = players, // Datasource.playersList,
dataUiModel = viewModel,
onCardClick = {it->
viewModel.setPlayerId(it)
navController.navigate(route= ClappsicScreen.Player.name)
}
)
}
Once an item in the list has been selected (player's card) I need to retrieve the item ID to query the DB to obtain exactly that item from the DB. with the same approach the DAO is not able to retrieve the player and crashes and no property delegate can be retrieved.
composable(
route = ClappsicScreen.Player.name
) {
val playerId = viewModel.getPlayerId()
val player by viewModel.getPlayer(playerId).collectAsState(initial = dbPlayer(id = playerId)) //val player here is THE initial dbPlayer, never retrieved the delegate correctly
PlayerScreen(player =player) //this crashes later
}
if I move to the top of the composable the getPlayer, the operation completes fine.
@Composable
fun ClappsicsApp(
viewModel: DataViewModel = viewModel(factory = DataViewModel.factory),
) {
val player by viewModel.getPlayer(playerId).collectAsState(initial = dbPlayer(id = playerId))
but ofc the result is not updated. Why the former code works and the latter no? How can I fix it?