galex
07/29/2020, 8:21 PM@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!")
}
Zach Klippenstein (he/him) [MOD]
07/29/2020, 8:30 PMIsval userRepository = UserRepository()
UserRepository
a regular constructor or function? If so, this should be remember { UserRepository() }
Similar here, this should beval user = mutableStateOf<User?>(null)
remember { mutableStateOf<User?>(null) }
If you want this coroutine to re-launch when the user ID changes, dolaunchInComposition {
launchInComposition(userUid) {
.state<User?> { null }
function, but that’s being deprecated and replaced by remember { mutableStateOf() }
.remember
calls, both your repository and that MutableState
object will be instantiated on every composition pass.launchInComposition
explains what the key
parameter is for: https://developer.android.com/reference/kotlin/androidx/compose/package-summary#launchincomposition_1galex
07/30/2020, 9:25 AMUserRepository
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 🙂