Roshan P Varghese
11/29/2020, 4:59 PMval isLoggedIn: StateFlow<Boolean> = mainRepository.userPreferencesFlow.map { it.isLoggedIn }.stateIn(viewModelScope )
The above code gives me the error: "Suspend function 'stateIn' should be called only from a coroutine or another suspend function"
I could do it with LiveData like below.
#LiveData
val isLoggedInLiveData: LiveData<Boolean> =
mainRepository.userPreferencesFlow.map { userPreferences ->
userPreferences.isLoggedIn
}.asLiveData()
Can anyone suggest an equivalent code for StateFlow?
``````
Slack ConversationJohn O'Reilly
11/29/2020, 5:10 PMstateIn
public suspend fun <T> Flow<T>.stateIn(scope: CoroutineScope): StateFlow<T> {
but want to use following version
public fun <T> Flow<T>.stateIn(
scope: CoroutineScope,
started: SharingStarted,
initialValue: T
): StateFlow<T>
John O'Reilly
11/29/2020, 5:11 PMval peopleInSpace = peopleInSpaceRepository.fetchPeopleAsFlow()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
Roshan P Varghese
11/29/2020, 5:13 PMRoshan P Varghese
11/29/2020, 5:15 PMval isLoggedIn: StateFlow<Boolean> = mainRepository.userPreferencesFlow
.map { it.isLoggedIn }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), false)