Rogelio Martinez
Rogelio Martinez

Reputation: 15

Why update() method from MutableStateFlow doesn't seem to work?

I'm actually creating a MutableStateFlow from a data class in this way

private val _uiState = MutableStateFlow(DataUiState())
val uiState: StateFlow<DataUiState> = _uiState.asStateFlow()

When calling in this from:

val newsItems = repository.dataItems(item)
_uiState.update {
    it.copy(dataItems = dataItems)
}

the method update {} shows a non-exist method error in compiler, why is this happening?

Upvotes: 1

Views: 2597

Answers (1)

Sergio
Sergio

Reputation: 30735

You need to import the function update to use it:

import kotlinx.coroutines.flow.update

...

val newsItems = repository.dataItems(item)
_uiState.update {
    it.copy(dataItems = newsItems)
}

Also make sure to import kotlinx-coroutines-core library:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'

Upvotes: 3

Related Questions