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
0 answers

MutableStateFlow observe and change value from anywhere without creating a coroutine

Android’s LiveData can be observed and changed anywhere without a coroutine with its observeForever() . How to do the same with MutableStateFlow that became a modern alternative to it (with Compose)? Currently I made a workable work-around but it…
0
votes
1 answer

Collecting flow 2 times from Room database

I have problem with collection songs from my database. Song Dao: @Query("SELECT * FROM song_table") fun observeSongs() : Flow> Repository: fun observeSongs() = songDao.observeSongs() ViewModel: private val…
Equlo
  • 115
  • 10
0
votes
1 answer

MutableStateFlow for methods

I’m trying to make my methods reactive to update the view in Compose. How to do it properly? interface Foo{ val a: Int fun bar(): Int fun foo(): MutableStateFlow = MutableStateFlow(bar() * a) } //@Composable val foo by…
Psijic
  • 743
  • 7
  • 20
0
votes
1 answer

Automatically update a flow from a changes of another flow (StateFlow) Jetpack Compose

I have a StateFlow from which my List composable collects any changes as a State. private val _people = MutableStateFlow(personDataList()) val people = _people.asStateFlow() And inside my viewModel, I perform modifications on _people and I verify…
z.g.y
  • 5,512
  • 4
  • 10
  • 36
0
votes
0 answers

Disable/cancel StateFlow for unit test

In my ViewModel there's a StateFlow which seems to prevent my unit test from ever completing, i.e. the test "hangs". I'm fairly new to the Flow lib and not sure how to cancel/disable said StateFlow so I can run my test as normal. I created a…
0
votes
3 answers

Stateflow collect multiple times inside each button click event. How to resolve this issue?

I checked and found that collects have been increased two times by each click . I don't know what is the cause and what is the remedy for below code . I am a beginner in Android dev and this issue becomes a headache for me. …
0
votes
1 answer

Can we replace all usages of StateFlow.value with StateFlow.update?

Reading the following articles, MutableStateflow Value Vs Update vs…
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
0
votes
1 answer

How to get updated results in StateFlow depending on parameter (sorting a list) with Jetpack Compose

I made a state with StateFlow with 2 lists. This is working good. I want to sort these lists according to a parameter that user will decide how to sort. This is my code in ViewModel: @HiltViewModel class SubscriptionsViewModel @Inject constructor( …
0
votes
0 answers

Migrating from livedata to kotlin flow

In the proccess of migration I found out some things that I would like to comment out with people who have good knowledge about this. To be honest, I am not master of flow, used livedata for long time, so now I would like to figure out as much as I…
0
votes
0 answers

Is it necessary to proxy data in viewModel with MutableStateFlow?

Recently in Android viewModel was useful because of liveData, but currently is it good to verbose your code and proxy all calls from Composable? Or it is fine to call subclass methods directly and left viewModel for merging modules like…
Psijic
  • 743
  • 7
  • 20
0
votes
2 answers

How to change the value of state object without using _?

In my app I perform a request to an API that should return a boolean. Inside the ViewModel class I have these 2 objects: private val _isAvailableState = mutableStateOf>(Success(false)) val isAvailableState: State>…
0
votes
2 answers

How to create custom event with the shortest way?

There is CustomWebViewClient with override function onPageFinished. What is the shortest way to notify MainViewModel about the function triggered? I mean some event. I suppose that can use StateFlow, something like this: class MainViewModel :…
Viewed
  • 1,159
  • 3
  • 16
  • 43
0
votes
1 answer

How to make StateFlow polymorphic in Androd?

I am at stage when I would like to expose my StateFlow() into the common BaseViewModel() class to implement common operations on it without repeating them in others implementations of ViewModels. I ended up with one idea, but faced with…
Joe Dow
  • 583
  • 1
  • 3
  • 12
0
votes
1 answer

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 = _uiState.asStateFlow() When calling in this from: val newsItems =…
0
votes
1 answer

How to hide or NOT SHOW items in a Room Database based on a Boolean

I have a Todo Application and I want to hide (which basically means not showing)the tasks based on its completed status(strikeThrough over the text). However, the hideCompleted tasks implementation I followed isn't working but the sort and search is…
Daniel Iroka
  • 103
  • 8