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

IsaacMart

08/01/2022, 12:49 PM
hello guys. I am retrieving a signed-in user from room database with the intention of updating views in compose. how should i store the user? I have tried remember{} but am getting the error:
Required:
MutableState<User.Companion>
Found:
User
Copy code
var user by  rememberSaveable { mutableStateOf(User) }

val systemUiController = rememberSystemUiController()
SideEffect {
    systemUiController.setStatusBarColor(
        color = PrimaryColor
    )
}

LaunchedEffect(key1 = context){
    viewModel.userResults.collect{ event ->
        when(event){
            is Resource.Success ->{
                  event.data?.let {// user from room database
                     user = it // It throws an error here
                 }
            }
            is Resource.Error ->{
                Toast.makeText(context, event.message, Toast.LENGTH_SHORT).show()
            }
        }

    }
s

Stylianos Gakis

08/01/2022, 1:45 PM
If you show the type of
var user
explicitly you’ll see that it’s of type
User.Companion
since you’re initializing it as such when you do
mutableStateOf(User)
. You have to decide what the type of
user
will be first. If you want it to be a nullable User object maybe you want to do
var user by rememberSaveable { mutableStateOf<User?>(null) }
.
i

IsaacMart

08/02/2022, 9:49 PM
I got a way around it. My problem was in the viewModel. I was using channels instead of mutableStateFlow so that i could collectAsFlow on the composable. Had to dig deep to understand to type of flows.Thanks for comming through.
8 Views