Regarding `collectAsState` is it more appropriate ...
# compose
c
Regarding
collectAsState
is it more appropriate to have 1️⃣
val user = appStateHolder.currentUser.collectAsState().value
or 2️⃣
val user = appStateHolder.currentUser.collectAsState()
a
for 2️⃣ you could do
Copy code
val user  by appStateHolder.currentUser.collectAsState()
right?
👍 2
👍🏻 1
c
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
whats the error? but whats
user.getsomeField
? hows that relate to
currentUser.collectAsStatE()
?e
c
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
Looks like it's a computed getter and not cached?
c
Hm. Just updated to user?.username and it works. Cool. Didn't think of using by
a
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
I have seen collectStateWithLifecycles from no win Android apps