Se7eN
10/31/2020, 10:55 AMText(user?.name ?: "")
make the code ugly?Grigorii Yurkov
10/31/2020, 10:58 AMuser
can be null?Se7eN
10/31/2020, 10:59 AMval user by viewModel.getUser(userId).collectAsState(initial = null)
User()
with empty strings as default values but not sure if that's the best wayGrigorii Yurkov
10/31/2020, 11:00 AMif (user == null){
Loading()
} else {
User(user)
}
Se7eN
10/31/2020, 11:02 AMGrigorii Yurkov
10/31/2020, 11:07 AMgetUser
returns Flow
?Se7eN
10/31/2020, 11:07 AMFlow<List<User>>
to a Flow<User>
Flow<List<User>>
fetches users from a network. In the home screen I'm displaying that List<User>
and when a User
item is clicked, I'm navigating to another screen and passing that User
's id. Then I'm loading that user by simply mapping the user list flow.val user: User by viewModel.getUser(userId).collectAsState(initial = emptyUser())
Grigorii Yurkov
10/31/2020, 11:24 AMonClick = {
scope.launch {
val user = viewModel.loadUserById(ID)
openUserScreen(user)
}
}
Se7eN
10/31/2020, 11:27 AMuser?.let { user ->
...
Text(user.name)
}
but I'm trynna save some indentation here.Grigorii Yurkov
10/31/2020, 11:30 AMSe7eN
10/31/2020, 11:31 AMGiorgos Neokleous
10/31/2020, 11:36 AMText(user?.name.orEmpty())
?Se7eN
10/31/2020, 11:38 AMText(user?.name.orEmpty())
CoilImage(user?.picture?.large.orEmpty())
// and a lot of other composables
Jeisson Sáchica
10/31/2020, 3:56 PM@Composable
fun UserScreen(user: User?) {
user?.let { UserInfo(it) } ?: EmptyComponent()
}
@Composable
fun UserInfo(user: User) {
Text(user.name.orEmpty())
CoilImage(user.picture?.large.orEmpty())
// and a lot of other composables
}
Se7eN
10/31/2020, 6:10 PM