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!

0 2035 5

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

mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
22 1618 20
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
2 1425 11
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 ...
7 3469 14
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
0 1284 2
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
0 2588 2
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
2 1163 8
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
0 1896 3
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
2 2080 9
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
0 1027 10
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
1 963 3
New

Other popular topics Top

Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
24 3382 25
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
105 13994 48
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
82 6591 31
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
34 3628 22
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
11 5087 5
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
31 3355 16
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
7 2839 4
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
62 6125 20
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
72 3682 22
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
1 1089 1
New

Latest in PragProg

View all threads ❯