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
27
votes
4 answers

LiveData Vs StateFlow: Should we switch from Live data to State Flow?

I have come across articles that recommend switching to StateFlow. Like the one here. Also in the new Android studio, StateFlow support is automatically included in the functionality of data binding, including the coroutines dependencies. Live data…
24
votes
1 answer

MutableStateflow Value Vs Update vs Emit

Let say I have a MutableStateFlow variable. What is the main differences and usage of the three cases mutable.value = 1 mutable.emit(2) mutable.update {3}
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
17
votes
2 answers

Using map on Kotlin's Stateflow

With LiveData inside a Viewmodel we use switchMap or Transformations.map like this val recipesList = cuisineType.switchMap { repository.getDisplayRecipes(it.cuisineType).asLiveData() } What would be the best way to do this with StateFlow? I know we…
alfietap
  • 1,585
  • 1
  • 15
  • 38
16
votes
2 answers

Use of SharedFlow in Android kotlin

Hey I am learning flow in kotlin. I am learning about MutableStateFlow and MutableSharedFlow. I tried to learn MutableStateFlow in real world example. But I am unable to get the MutableSharedFlow example, which place it suits more. I tried some…
13
votes
2 answers

Android - How to read a value from a kotlin flow?

I have a flow of Episode from a room database. I can observe as livedata this flow with no problem. But I also would like to read the last value from this flow when the user clicks on a button. I tried with first() terminal flow operator but it…
u2gilles
  • 6,888
  • 7
  • 51
  • 75
12
votes
2 answers

StateFlow don't emit when the new value same last value

I have a login form. I use StateFlow to send LoginResult (after call API) from ViewModel to Activity. In the Activity, I will show an error dialog if login failed. It works well for the first time but from the second time I login failed, the error…
Linh
  • 57,942
  • 23
  • 262
  • 279
8
votes
5 answers

StateFlow last value is collected again in ui

So lately I've been working with StateFlow, SharedFlow, and Channels API's but I'm struggling with one common use case while trying to migrate my code from LiveData to StateFlow in the presentation layer. The problem I'm facing is when I emit my…
7
votes
1 answer

Expose value from SharedPreferences as Flow

I'm trying to get a display scaling feature to work with JetPack Compose. I have a ViewModel that exposes a shared preferences value as a flow, but it's definitely incorrect, as you can see below: @HiltViewModel class MyViewModel @Inject…
7
votes
2 answers

Unit testing viewModel that uses StateFlow and Coroutines

Kotlin 1.4.21 I have a very simple ViewModel that uses coroutine and stateFlow. However, the unit test will fail as the stateFlow doesn't seem to get updated. I think its because the test will finish before the stateFlow is updated. expected not to…
ant2009
  • 27,094
  • 154
  • 411
  • 609
7
votes
2 answers

Combine two kotlin flows into a single flow that emits the latest value from the two original flows?

If we have two flows defined like this: val someflow = flow { emit("something") } and another flow defined like: val stateFlow = MutableStateFlow("some value") Is it possible to combine the two flows into a single flow that just emits the last…
Citut
  • 847
  • 2
  • 10
  • 25
7
votes
2 answers

Populate and emit StateFlow List

I want to use StateFlow. But for now, I don't find any lecture which could help me. I'm facing an issue : To start, I have a singleton which hold a list of String, I want something "easy" to understand even if it isn't the goal purpose for now. The…
JohnDoe
  • 83
  • 1
  • 6
6
votes
2 answers

Best practice when using mutableState with jetpack compose inside viewmodel with exposing mutable state

I have the following ViewModel @HiltViewModel class ShareViewModel @Inject constructor( private val taskRepository: TaskRepository ): ViewModel() { private val searchAppBarStateMutableState: MutableState =…
ant2009
  • 27,094
  • 154
  • 411
  • 609
6
votes
1 answer

How to use DataStore with StateFlow and Jetpack Compose?

I try to give the user the choice whether to use the UI Mode 'light', 'dark' or the 'system' settings. I would like to save the selection as DataStore. The drop down menu for the user selection is not loading the value from DataStore. It is always…
6
votes
3 answers

flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) doesn't stop flows while App is in background

I'm trying to observe the result of the View Collection and upstream flows stopped. But viewModel.testFlow is still collecting while the App is in the background. Why can't I observe the collection is stopped? Am I observing something…
6
votes
2 answers

Flow doesn't update Composable

I faced the following problem: There's the registration screen, which has several input fields on it. When a user enters something, the value is passed to ViewModel, set to screen state and passed back to the screen via StateFlow. From the…
1
2 3
12 13