
dachristenson
Kotlin and Android Development featuring Jetpack: Transformations class doesn't seem to exist anymore (pp. 140-141
@mfazio23
Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googling around reveals that ANOTHER change has been made, and that Transformations no long exists as such but is now built-into Kotlin in some way. I found an example about how to change Transformations.mapswitch() to someClass.mapswitch() but no complete example about how to do the same with Transformations.map(). My efforts to apply the same format to all of the Transformations.map() statements just changed the error from “Unresolved reference: Transformations” to “Type mismatch. Required: Player. Found: Player?”, for instance. This despite setting “lc_version” to “2.3.0”.
What should I do to get Chapter 5’s code working?
Thanks in advance!
Marked As Solved

mfazio23
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!
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /react-native
- /v
- /wasm
- /django
- /security
- /nodejs
- /centos
- /rails
- /haskell
- /fable
- /gleam
- /js
- /swift
- /deno
- /tailwind
- /assemblyscript
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /preact
- /flutter
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /emacs
- /vuejs
- /nim
- /elm
- /nerves