I have found myself writing some snippets similar ...
# getting-started
c
I have found myself writing some snippets similar to this. Is there a better way to do it?
Copy code
fun updateUser(username: String? = null, email: String? = null, token: String? = null) {
  if (username != null) {
    _appUser.value = _appUser.value?.copy(username = username)
  }
  if (email != null) {
    _appUser.value = _appUser.value?.copy(email = email)
  }
  if (token != null) {
    _appUser.value = _appUser.value?.copy(authToken = token)
  }
}
e
you could do just one copy,
Copy code
if (username != null || email != null || token != null) (
    val appUser = _appUser.value
    _appUser.value = appUser.copy(
        username = username ?: appUser.username,
        email = email ?: appUser.email,
        authToken = token ?: appUser.authToken,
    )
}
c
Cooll. okay so not far off from what i had. thanks!
f
If _appUser is a mutable state flow, consider using the update extension function to update it
i
You can use the let extension to get rid of the if something is null check
c
_appUser IS a mutablestateflow!