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
4
votes
1 answer

Unlike Livedata, using Flow in Room query doesn't trigger a refresh when updating a table entry but works if I delete or insert an entry

When using Livedata as a return type for a select* query on a table in Room, then I observe on it, I get triggers if I update/insert/delete an entry in that table. However, when I tried using Kotlin Flow, I only get 2 triggers. The first trigger…
Mena
  • 3,019
  • 1
  • 25
  • 54
3
votes
0 answers

How to collect a stateflow from a Custom View?

This article explains how to use ViewModels in custom views : class SummaryView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) { private val viewModel by lazy { …
Greelings
  • 4,964
  • 7
  • 34
  • 70
3
votes
1 answer

Android ViewModel with StateFlow - testing issue. Test never waits for next value

I have search view model like this. searchPoiUseCase doing requests to Room DB. For testing purposes i am using Room.inMemoryDatabaseBuilder. @HiltViewModel class SearchVm @Inject constructor( private val searchPoiUseCase: SearchPoiUseCase ) :…
3
votes
1 answer

Flow.stateIn() not receiving new value from it's source

I tried to combine several SharedFlow into one StateFlow using stateIn. But my StateFlow seems not updating after I emit new value to it's SharedFlow source. I found out that the problem is from how I used stateIn. Here is the simplified code I am…
3
votes
0 answers

StateFlow triggers multiple times when used to navigate to other Composable

I have been using StateFlow + sealed interfaces to represent the various UI states in my Android app. In my ViewModel I have a sealed interface UiState that does this, and the various states are exposed as a StateFlow: sealed interface UiState { …
3
votes
6 answers

How to avoid default value in MutableStateFlow kotlin

I am using MutableStateFlow in my project. When we initialise the MutableStateFlow object we need to give default value. val topics = MutableStateFlow>(emptyList()) when I collect this value [null, "Hello", "world"] I want to pass…
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
3
votes
2 answers

How to cancel a combine of flows when one of them emits a certain value?

I am doing multiple network requests in parallel and monitoring the result using a Stateflow. Each network request is done in a separate flow, and I use combine to push the latest status on my Stateflow. Here's my code: Repo class: fun…
Mena
  • 3,019
  • 1
  • 25
  • 54
3
votes
1 answer

Flow emits different values when collecting it multiple times

I created a Flow from which I emit data. When I collect this flow twice, there are 2 different sets of data emitted from the same variable instead of emitting the same values to both collectors. I have a simple Flow that I created myself. The text…
3
votes
4 answers

Kotlin Coroutine/Flow Timeout without cancelling the running coroutine?

I am trying to create a Flow that emits a value after a timeout, without cancelling the underlying coroutine. The idea is that the network call has X time to complete and emit a value and after that timeout has been reached, emit some initial value…
Citut
  • 847
  • 2
  • 10
  • 25
3
votes
2 answers

jetpack compose stateflow not refreshing list

In my project, I have two fragments. One is a normal fragment, and another is a bottomSheetDialogFragment. In my normal fragment, I have a lazyColumn. I would like to do some settings in the bottomSheetDialogFragment and create a new Hiit object and…
Fever
  • 157
  • 2
  • 9
3
votes
3 answers

How can we save and restore state for Android StateFlow?

We can create LiveData or StateFlow in a similar way as below val _liveData = MutableLiveData(0) val _stateFlow = MutableStateFlow(0) But In LiveData, we can ensure the data is saved and restored by using val _liveData: MutableLiveData =…
Elye
  • 53,639
  • 54
  • 212
  • 474
3
votes
1 answer

Collect transformed StateFlow in Composable

There is function collectAsState() applicable to a StateFlow property in order to observe it in a Composable. A composable requires a StateFlow because StateFlow guarantees an initial value. A Flow doesn't come with that guarantee. Now, what is the…
3
votes
0 answers

How to handle a lot of api requests and observe them in kotlin Coroutines

I want to call a request to an API in a loop and observe the result for each api requests. The problem is that I think some result are got lost. How can I handle this? I want to handle this using flow or stateflow. Is it better to wait to get result…
3
votes
2 answers

Suspending until StateFlow reaches one of the desired states and returning the result

Consider a sealed class State. sealed class State { object Unknown : State() object Loading : State() object Success : State() data class Failure(val exception: Exception) } I have a stateflow where consumers can actively listen to…
2
votes
1 answer

Jetpack Compose - have a problem with recomposition

I am using a ViewModel to store data. The ViewModel has a StateFlow class that list of Users. There is also a donut:Int field. In Compose, when I click on the Donut-button, the variable "donut" is incremented and the recomposition is successful -…
1 2
3
12 13