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

New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Latest in PragProg

View all threads ❯