Questions tagged [kotlin-stateflow]

In Kotlin coroutines, a StateFlow is a mutable value whose changes can be observed as a flow. Use this tag for questions about publishing, transforming and consuming state flows.

A SharedFlow that represents a read-only state with a single updatable data value that emits updates to the value to its collectors. A state flow is a hot flow because its active instance exists independently of the presence of collectors. Its current value can be retrieved via the value property.

source

194 questions
0
votes
3 answers

Best approach collecting Callback Flow in viewmodel with Kotlin (Jetpack Compose)

I'm trying to collect callback flow from repository in viewmodel to save user Data. I have this two implementations: CODE A) private val _userState = MutableStateFlow(null) val userState: StateFlow = _userState init { …
0
votes
0 answers

In Kotlin/Android, the correct value does not come from the ViewModel and is not updated after recompostion occurred

An expense item list is retrieved from the Room database, as shown below. And I rearranged them by date using '.flatMapConcat {}' in the Repository. override fun getMonthlyStream(): Flow> { return dbDao.getMonthlyData("04") …
0
votes
1 answer

Communicate from composable to fragment using viewmodel

Communicate from composable to fragment using viewmodel I am having a Composable @Composable fun SimpleBottomButton(SCVM: StudioComposeViewModel) { Button(onClick = { SCVM.setbottomButtonClick(2) }) { Text(text = "Bottom…
1234567
  • 2,226
  • 4
  • 24
  • 69
0
votes
2 answers

Is StateFlow usable with Room? If yes, how?

I'm currently looking for ways to implement sorting on Room. Previously, I was using Flow with "getAll" that contains a MutableList which becomes a LiveData on the ViewModel. Which works perfectly, then I began working on sorting. After researching…
rminaj
  • 560
  • 6
  • 28
0
votes
1 answer

Retrigger collection from StateFlow/SharedFlow from Activity in Jetpack compose

Activity code which is collecting the flow as state and updating the UI based on the state emitted by the Flow. setContent { val viewModel: MainViewModel = viewModel() val state: State = viewModel.flow.collectAsStateWithLifecycle( …
0
votes
0 answers

Android ViewModel unable to collect after initial value is emitted via `StateFlow`

I am migrating my Android project to Jetpack Compose. To do so, I emit events with StateFlow from a separate singleton class from Compose and collect it in my ViewModel. However, after one event is emitted nothing else is collected from the flow…
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
0
votes
1 answer

how to filter Android StateFlow in Alphabetical order

how to filter Android StateFlow in Alphabetically order I have a StateFlow which gives the result based on names, I want to filter it to show the names Alphabetically how can we do this var filterFlow = MutableStateFlow("") var filterValue:…
1234567
  • 2,226
  • 4
  • 24
  • 69
0
votes
0 answers

Improve ViewModel MutableStateFlow updates

Currently, most of my ViewModels are duplicating code to update the UI state by functions like this one: private fun removeErrorIfAny() { _uiState.value.error?.let { _uiState.update { it.copy(error = null) } …
Ygor
  • 188
  • 8
0
votes
1 answer

How to correct use of the StateFlow value?

Today I faced the question of how to write the code correctly. I have a Fragment in which I call the viewModel function viewModel.restorePassword(). There is a variable in viewModel: private var _currentNumberPhone = MutableStateFlow("") val…
0
votes
0 answers

Cannot create an instance of class error in Kotlin ViewModel

I am trying to create a ViewModel in my Kotlin app, but I am getting the following error: java.lang.RuntimeException: Cannot create an instance of class com.ri.movieto.presentation.home.HomeViewModel. Also, I am using Hilt. And here is the code for…
0
votes
1 answer

MutableStateFlow does not recomposes value

I have a method that updates local value if I got a spicific json: override fun onGameStartOpenWord(startOpenWord: StartOpenWord) { var word = _state.value.gameState!!.words.find { startOpenWord.word.id == it.id } ?: return val tempCopy =…
AdisAlagic
  • 436
  • 2
  • 8
0
votes
1 answer

New card item appearse only after phone rotation

I am writing a small gallery app for my cat. It has a button by clicking on which a new PhotoItem is added to the displayed list, but it appears only after phone rotation and I want it to appear on the screen right after button was clicked. Right…
0
votes
1 answer

would coroutines .combine wait until all flows have data?

morning all, let's say I have this code internal val canAllowProcess: StateFlow = combine( _isLoading, _isEligible ) { arg1, arg2 -> // do something here }.stateIn( scope = viewModelScope, // how long…
0
votes
2 answers

Can't get previous emitted values from Flow

Can't get previous emitted values from Flow. class TestActivity: ComponentActivity() { ... private val flowA = MutableStateFlow(0) private val flowB = MutableStateFlow("") init { flowB.onEach { Log.d("flowtest", "test - $it") } …
0
votes
0 answers

How do I check the value changed in unit test (Android)

`I am having a hard time getting the middle? value of a stateFlow and checking using assertTrue Here is my code for testing fun initData() = viewModelScope.launch { modifiableUiState.emit(UiState.Loading) try { testSomeThing() …