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

Android : Jetpack Compose : How to get notified on value change of StateFlow inside Composable?

I have a ViewModel which has the following list as StateFlow private val _mutableStateTopicItemList = MutableStateFlow>(listOf()) val stateTopicItemList = _mutableStateTopicItemList.asStateFlow() Now inside my Composable function, I…
0
votes
1 answer

Dont change stateflow in viewmodel

My viemodel: Class CycleVM{ private val _cycle = MutableStateFlow(Cycle()) override val cycle: StateFlow = _cycle.asStateFlow() override fun updateImg(imgStr: String) { val c = cycle.value.copy(cycle.value) …
0
votes
0 answers

Compose use MutableStateFlow not updating

My code doesn't work properly and I can't find where the problem is. // MutableStateFlow create // TestService.kt val flowState = MutableStateFlow(true) class TestService : Service() { override fun onBind(p0: Intent?) = null private val…
0
votes
1 answer

Jetpack Compose: extremely long frames after lots of input

Link to my code: https://github.com/tylerwilbanks/word-game-android After around maybe 10 seconds after startup, if you tap keyboard buttons quickly, suddenly the app performance will tank with 5-10 second frame processes. The issue does not seem to…
0
votes
1 answer

Properly use a loop in coroutine to place rest api call

I am currently building a Flickr-like app and I do have a question about pagination. I am currently calling a FlickrApi to retrieve the recent photos. It's working well but I only get the first 100 because the api only returned list of photos using…
Seb
  • 2,929
  • 4
  • 30
  • 73
0
votes
1 answer

Testing MutableStateFlow in Android Compose?

I have the following code: ViewModel: private val _prefix = MutableStateFlow("") val prefix: StateFlow = _prefix private val _firstName = MutableStateFlow("") val firstName: StateFlow = _firstName private val…
0
votes
4 answers

How to process data asynchronously using coroutines?

I need to execute 4 parallel requests. Here is my code: suspend fun fetchAsyncData() = coroutineScope { val first = async { repository.taskFirst() } val second = async { repository.taskSecond() } val third = async {…
0
votes
0 answers

android testing state flow test block

I am writing tests for my view model and I encountered an issue, did some research and I found this on docs and using that I fixed my problem but have my question unanswered https://developer.android.com/kotlin/flow/test#turbine @Test fun…
nasibeyyubov
  • 1,735
  • 3
  • 11
  • 28
0
votes
1 answer

A simple problem in Jetpack compose regarding the update list without the update UI

I am using LazyColumn to represent a list of names containing an Index, and its data source is a StateFlow>. I find that if I add elements to this MutableStateFlow list, the interface is updated immediately. But if I modify the…
progquester
  • 1,228
  • 14
  • 23
0
votes
2 answers

Composable Not Collecting Updated Value in StateFlow

I have a form screen written in Compose where the user can fill out some TextFields and submit it. If they press the submit button before filling out the fields, the supporting text for the empty TextFields will change to say "Required". Required…
pjp94
  • 13
  • 3
0
votes
3 answers

Is it an anti-pattern to keep a StateFlow inside a Repository?

I have a singleton LocationRepository class and inside that I have a callbackFlow method which gets location updates from FusedLocationProvider. And a Service class that collects that flow. The thing is I want to use that location info in an…
0
votes
1 answer

Observe updated StateFlow List from ViewModel in Composable Function

I am trying to update a list item's value and then observing the updated list as a state in the Composable function. The problem is that if I emit a new list then it's observing as expected but not working when I emit the updated list in ViewModel.…
0
votes
1 answer

android kotlin Flow emitted values are not collected

I have model class: data class TweetResponse( val data: Data?, val matching_rules: List?, val lifeSpan: MutableStateFlow? = MutableStateFlow(30), var timer: CountDownTimer? ) In my repository class I get list…
Kratos
  • 681
  • 1
  • 13
  • 30
0
votes
0 answers

Is it thread safy to modify a mutable variable for a mutableStateFlow in producer and consumer concurrently?

Refer to following codes: @Test fun test() = runBlocking { val tag = testName.methodName var mutFlow = MutableStateFlow(mutableListOf()) val imFlow = mutFlow.asStateFlow() Log.d(tag, "mutable value is…
progquester
  • 1,228
  • 14
  • 23
0
votes
1 answer

StateFlow only works from launch but not from repeatOnLifecycle

By the docs, I should never collect the flow from launch or launchIn, but if I try to change my code to be executed from inside the repeatOnLifecycle it is just being ignored. This is the code I'm using right now in my Fragment: private fun…
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100