Jetpack Compose collectAsState issue


I’m using Composes collectAsState() function to collect from this StateFlow

val _authToken = MutableStateFlow(AuthToken("", 0))
val authToken: StateFlow<AuthToken> = _authToken
val authToken by loginViewModel.authToken.collectAsState() // this returns AuthToken which is fine.

However, when turning a cold flow into a stateflow using the stateIn operator then using collectAsState() it returns a State version of the object which is odd then I need to call .value on collectAsState() to retrieve it.

Does anyone know why this happens?

val user = repository.getUser(viewModelScope).stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)
val user = profileViewModel.user.collectAsState() // returns State<User>



val user = profileViewModel.user.collectAsState() 


val user by profileViewModel.user.collectAsState() 

Answered By – Bencri

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published