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

null cannot be cast to non-null type RecyclerView with stateFlow

I got some categories from an api and trying to show them on a recycler view but it doesn't work for some reason. Although the data appears correctly in the logcat, it is sent as null to the Category adapter. This is the Main Activity (where I'm…
0
votes
1 answer

How to update the data passed to the adapter without recreating it android kotlin

Faced such a problem: I have a RecyclerView, the data for which I get from the ViewModel using StateFlow: viewModel.items.collect { setRecyclerView(items) } Then, let's say, somewhere inside the Fragment, I change the data for items and there…
0
votes
1 answer

How to collect StateFlow states from another class?

I have view model like this: class SimpleViewModel : ViewModel() { private val _state = MutableStateFlow(false) val state: StateFlow = _state } How can I collect this state's values and call methods from another class like…
sss sss
  • 154
  • 6
0
votes
1 answer

Update of StateFlow not propagating to the Jetpack Compose UI

I'm trying Jetpack Compose on Android with a viewmodel and StateFlow on a super small game application, and I've followed the codelabs, but when I update my state, nothing happens on the UI. I'm sure I'm missing something stupid, but I'm unable to…
0
votes
2 answers

How can I use Retrofit to parse a JSON response from an API, that has an embedded JSON object?

I'm trying to learn Android Development by making a bus arrival timing application that makes API calls to a local API that has the arrival timings for the next 3 buses. I am using Kotlins and Jetpack Compose to help. This is a sample of the JSON…
0
votes
1 answer

StateFlow is updated but not collected

I'm currently learning about the new Android stack (MVVM, compose, kotlin Flow/StateFlow), and I'm having trouble debugging a StateFlow where the value is updated, but I have no sign of collection from the composable. It's a generic question, but I…
A.Danibo
  • 494
  • 1
  • 4
  • 16
0
votes
1 answer

How this change from runBlocking to sharedFlow work?

I would like to ask you why does it work? Normally when I used collectLatest with flow my data wasn't collected on time and the return value was empty. I have to use async-await coroutines, but I have read it blocks main thread, so it is not…
0
votes
1 answer

Android compose multiple stateflows recomposition

Still new to compose + kotlin, so I'm having some trouble getting stateflow working. There might be some fundamentals that I don't understand or missing some function. Problem: I have two stateflows in my view model and both would trigger a…
0
votes
1 answer

Android. How to implement countdown timer inside fragment?

I'm trying to implement simple countdown timer inside fragment (without view model or use case). Here is my code: private fun startTimer(totalSeconds: Int): Flow = (totalSeconds - 1 downTo 0) .asFlow() .onEach { delay(1000)…
0
votes
1 answer

State flow is not collecting emitted items

Imagine following scenario: I open Search View and SearchViewModel is initialized class SearchViewModel( usecase: Usecase ) : ViewModel() { init { viewModelScope.launch { usecase.initialize() } } fun…
murt
  • 3,790
  • 4
  • 37
  • 48
0
votes
1 answer

Can I replace the Flow with StateFlow for a Counter that is Lifecycle Aware

I have a counter logic using Flow in ViewModel, and auto increment. class MainViewModel( private val savedStateHandle: SavedStateHandle ): ViewModel() { val counterFlow = flow { while (true) { val value =…
Elye
  • 53,639
  • 54
  • 212
  • 474
0
votes
1 answer

realTime List using callbackFlow from firestore

i'm facing hard times updating list of Orders in real time from firestore using stateflow !! class RepositoryImp : Repository { private fun Query.snapshotFlow(): Flow = callbackFlow { val snapshott = addSnapshotListener {…
0
votes
0 answers

How to test a stateflow that updates (emits) infinitely?

See this answer for "Unit test the new Kotlin coroutine StateFlow" and this issue in Kotlin coroutines GitHub repo.. How can I test all emissions of this StateFlow (results variable)? class Emitter(dispatcher: CoroutineContext) { private val…
Mahozad
  • 18,032
  • 13
  • 118
  • 133
0
votes
1 answer

Retrieving latest value of a Flow which is not a StateFlow

How can I get the latest value of a Flow? I don't have a StateFlow where I need that latest value. This is the condensed scenario: There is a repository exposing a StateFlow val repositoryExposedStateFlow: StateFlow =…
ceedee
  • 371
  • 1
  • 11
0
votes
1 answer

How to save mutable state of TextField JetpackCompose

Description: It's pretty basic problem but I've tried few solutions and nothing is working for me. I simply want to save state of BasicTextField in ViewModel. I tried mutableStateOf("SampleText") and text appears in BasicTextField but is not…