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

galex

07/29/2020, 8:21 PM
Ok I might be too tired to work on this at 11pm instead of going to sleep, but what is wrong with this that the load of the value doesn’t trigger recomposition?
Copy code
@Composable
fun ProfileScreen(userUid: String) {

    val userRepository = UserRepository()
    val user = mutableStateOf<User?>(null)

    launchInComposition {
        user.value = userRepository.get(userUid)
        Log.d(TAG, "url = ${user.value?.profileUrl}")
    }

    user.value?.profileUrl?.let { CoilImage(data = it) } ?: Text("No Image!")
}
z

Zach Klippenstein (he/him) [MOD]

07/29/2020, 8:30 PM
val userRepository = UserRepository()
Is
UserRepository
a regular constructor or function? If so, this should be
remember { UserRepository() }
val user = mutableStateOf<User?>(null)
Similar here, this should be
remember { mutableStateOf<User?>(null) }
launchInComposition {
If you want this coroutine to re-launch when the user ID changes, do
launchInComposition(userUid) {
.
🔥 1
👍 1
1
There’s also a
state<User?> { null }
function, but that’s being deprecated and replaced by
remember { mutableStateOf() }
.
Without the
remember
calls, both your repository and that
MutableState
object will be instantiated on every composition pass.
👍 2
The documentation on
launchInComposition
explains what the
key
parameter is for: https://developer.android.com/reference/kotlin/androidx/compose/package-summary#launchincomposition_1
g

galex

07/30/2020, 9:25 AM
Thank you for your explanations, it starts to become clearer! Here
UserRepository
is a new Object which will be passed to this Composable. And yes I misued
launchInComposition
, I thought that as it sounds like
launch
it’s the same signature but no 🙂