tl;dr: Change to calling map { ... }
on the LiveData
object, change the variable to its nullable version.
It seems like you are on the right track with how you tried to fix this. With v2.6.0 of the Lifecycle library, the Transformations
was completely removed (which you unfortunately had to find out the hard way), but thankfully the code is simpler now.
For a concrete example, this code:
this.currentPlayer = Transformations.map(this.currentGame) { gameWithPlayers ->
gameWithPlayers?.players?.firstOrNull { it.isRolling }
}
is now
this.currentPlayer = this.currentGame.map { gameWithPlayers ->
gameWithPlayers?.players?.firstOrNull { it.isRolling }
}
But, as you discovered, this causes issues with nullability due to how newer versions of LiveData
handle null values. Luckily, this is an easy enough fix; change the LiveData
variable to have a nullable type inside of it.
Specifically, this means:
val currentPlayer: LiveData<Player>
becomes
val currentPlayer: LiveData<Player?>
That should resolve the issues you have, let you move forward, and get rid of the Transformations
class which was never my favorite in the first place!