https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

07/11/2022, 12:30 AM
Regarding
collectAsState
is it more appropriate to have 1️⃣
val user = appStateHolder.currentUser.collectAsState().value
or 2️⃣
val user = appStateHolder.currentUser.collectAsState()
a

agrosner

07/11/2022, 12:36 AM
for 2️⃣ you could do
Copy code
val user  by appStateHolder.currentUser.collectAsState()
right?
👍 2
👍🏻 1
c

Colton Idle

07/11/2022, 1:06 AM
Hm. Could you? The way I stumbled upon this was that I was doing a bunch of user != null checks while using option 2. The IDE told me that the checks weren't necessary so I removed them and then figured out that I was doing a != of my stateFlow and not the actual value of the stateFlow.
Tried that and although it compiled, I couldn't actually do
user.getSomeField
due to an error.
a

agrosner

07/11/2022, 1:12 AM
whats the error? but whats
user.getsomeField
? hows that relate to
currentUser.collectAsStatE()
?e
c

Colton Idle

07/11/2022, 1:24 AM
I tried
user.username
for example and I got a compile error of
Copy code
"Smart cast to 'AppUser' is impossible, because 'user' is a property that has open or custom getter"
a

agrosner

07/11/2022, 1:25 AM
Looks like it's a computed getter and not cached?
c

Colton Idle

07/11/2022, 1:28 AM
Hm. Just updated to user?.username and it works. Cool. Didn't think of using by
a

agrosner

07/11/2022, 1:46 AM
By just unwraps the state property using a delegate and tells compose of its changes. So you would have same thing if you used = and value accessor
i

itnoles

07/11/2022, 3:13 AM
I have seen collectStateWithLifecycles from no win Android apps
24 Views