dachristenson

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

mfazio23

Author of Kotlin and Android Development featuring Jetpack

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 Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New

Latest in PragProg

View all threads ❯