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
1
vote
1 answer

Android Two-way Data Binding Using MutableStateFlow and Observe Changes in ViewModel

Just learned MutableStateFlow, trying to replace MutableLiveData. Also Android doesn't recommend that we observe LiveData inside ViewModel (unless you use observeForever(observer) but you need to remember to remove it so it's kind of…
1
vote
1 answer

Problem with state in jetpackCompose and Flow

I have a problem for now in JetpackCompose. The problem is, when I'm collecting the Data from a flow, the value is getting fetched from firebase like there is a listener and the data's changing everytime. But tthat's not that. I don't know what is…
1
vote
0 answers

How to change data source of kotlin StateFlow

I am migrating this code sample to StateFlow. class RosterMotor(private val repo: ToDoRepository) : ViewModel() { private val _states = MediatorLiveData() val states: LiveData = _states private var lastSource:…
Vivart
  • 14,900
  • 6
  • 36
  • 74
1
vote
1 answer

In the lifecycle scope with two StateFlow observers, only first works

It seems to me that I do not quite understand something. Could you please explain to me why when I use this example, only the first collect works for me. lifecycleScope.launch { viewModel.test1.flowWithLifecycle(lifecycle,…
1
vote
1 answer

Calling multiple viewmodel methods from launchWhenStarted does not work

This code invokes two methods on the same viewmodel and listens for updates. But only the first method completes, the second does not event trigger. private fun initData() { lifecycleScope.launchWhenStarted { …
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
1
vote
0 answers

What is the better way to use turbine to test coroutine StateFlow

I am looking for a better way to test coroutine. Currently, I'm using Turbine as the test helper. However, I have some difficulty improving the timeout issue. ex: class RepositoryTest { @get:Rule val rule = InstantTaskExecutorRule() …
1
vote
1 answer

StateFlow collects in one coroutine

I tried init three collects in one coroutines, but worked only first. Only when i set collects in different coroutines its work. Why? lifecycleScope.launch { launch { homeViewModel.dateStateFlow().collect { date -> …
MXF
  • 79
  • 1
  • 9
1
vote
0 answers

Receiving ResponseAlreadySentException after attempting to respond to ApplicationCall objects passed through StateFlow

I am testing an event driven architecture in KTOR. My Core logic is held in a class that reacts to different Event types being emitted by a StateFlow. EventGenerators push Events into the StateFlow which are picked up by the Core. However, when the…
ITJscott
  • 522
  • 4
  • 17
0
votes
2 answers

check response request in viewmodel MutableStateFlow mvi

I'm currently working on an Android application utilizing the MVI (Model-View-Intent) architecture. In my ViewModel, I'm using a MutableStateFlow to manage the UI state. I want to optimize my code to prevent repetitive work when checking response…
0
votes
0 answers

State Flow to broadcast app state to different screens at the same time

I'm developing a fitness app and I use a wrapper around some data classes that contains information shared by all the screens , like info for the current user , what settings are used by the app etc., My initial idea was to retrieve this information…
0
votes
1 answer

StateFlow in RecyclerView emitting same values for every row

I have an Android ViewModel that manages a recyclerView with ImageView in each row where I'm setting a userId string in a StateFlow. I want to use that userId to retrieve the user's description as another StateFlow. I'm doing this: class…
VIN
  • 6,385
  • 7
  • 38
  • 77
0
votes
0 answers

Jetpack Compose: State inside ViewModel and record in Database is updated, but Composable is not collecting the new state value

I'm new to jetpack compose, this is my first project so far and I'm using StateFlow to update the compose views: Model: @Parcelize @Entity data class User( var firstName: String = "", var lastName: String = "", var gender: String = "", …
0
votes
3 answers

How exactly does Kotlin conflate() work on hot flows (StateFlow)?

My understanding is that conflate() emits only the latest value when applied to a flow. How is the "latest value" determined since the underlying flow is a hot observable? At any point, it can't know if more emissions are coming but it doesn't just…
0
votes
0 answers

Kotlin multiplatform Stateflow is not collected in Desktop application

In a kotlin multiplatform app I want for the desktop application to handle the "navigation" between different panes without the help of any Library. I have one window with two panes: the first pane displays a list of names the second pane is…
eqtèöck
  • 971
  • 1
  • 13
  • 27
0
votes
1 answer

Should we set LiveData and/or kotlin Flows to null in ViewModel onCleared?

I figured that since both LiveData & Flow should be lifecycle aware that there is no reason to do this. Is there any benefit or detriment to setting them to null yourself in onCleared?