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

Stateflow value not getting being observed in data binding

I have a stateflow that is giving me the value of a mutable state flow from ViewModel, what I am trying to do is, I want to show hide a webview based on a button click. When the value is true I want to show the web view and when I want to hide it I…
5
votes
1 answer

Jetpack compose lazy column not recomposing with list

I have a list which is stored inside a Viewmodel via Stateflow. class FirstSettingViewModel : ViewModel() { private val _mRoomList = MutableStateFlow>(mutableListOf()) val mRoomList: StateFlow> = _mRoomList …
5
votes
1 answer

Is collectAsStateWithLifecycle only applicable to cold flow, and not helpful for hot flow (e.g. stateFlow)?

Lately, we have a new API since android lifecycle library version 2.6.0-alpha01, i.e. collectAsStateWithLifecycle(...) It is advocated by Google Developer in this article If you’re building an Android app with Jetpack Compose, use…
Elye
  • 53,639
  • 54
  • 212
  • 474
5
votes
0 answers

Jetpack Compose navigation state doesn't restore

I'm struggling with the Jetpack Compose navigation. I'm following the NowInAndroid architecture, but I don't have the correct behaviour. I have 4 destinations in my bottomBar, one of them is a Feed-type one. In this one, it makes a call to Firebase…
5
votes
2 answers

Android LiveData/StateFlow List Item Property Update Issue

So I'm updating my RecylerView with StateFlow like following: My data class: data class Student(val name: String, var isSelected: Boolean) My ViewModel logic: fun updateStudentsOnSelectionChanged(targetStudent: Student) { val targetIndex…
Sam Chen
  • 7,597
  • 2
  • 40
  • 73
5
votes
1 answer

How to print size of flow in kotlin

Hey I am new in kotlin flow. I am trying to print flow size. As we know that list has size() function. Do we have something similar function for flow. val list = mutableListof(1,2,3) println(list.size) output 2 How do we get size value in…
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
5
votes
1 answer

A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction

Please help for the build error. Kotlin version : 1.5.31 Gradle-zip: 7.2 Gradle version: 7.0.3 Execution failed for task ':app:mergeDebugJavaResource'. A failure occurred while executing…
5
votes
3 answers

How to properly use StateFlow with Jetpack compose?

I'm doing a API call in the ViewModel and observing it in the composable like this: class FancyViewModel(): ViewModel(){ private val _someUIState = MutableStateFlow(FancyWrapper.Nothing) val someUIState: StateFlow
5
votes
2 answers

LiveData Transformation to StateFlow / SharedFlow

What is the equivalent code for this live data transformation in StateFlow / SharedFlow? val myLiveData: LiveData = Transformations .switchMap(_query) { if (it == null) { …
4
votes
1 answer

Android collectAsStateWithLifecycle() not collecting inside composable

I am using compose LazyColumn with viewModel updating the list items by having inside my viewModel: data class ContactsListUiState( val contacts: MutableList ) @HiltViewModel class ContactsViewModel @Inject…
4
votes
3 answers

MutableStateFlow not working with MutableList

Here is my MutableStateFlow value I try to work with: val songList: MutableStateFlow> = MutableStateFlow(arrayListOf()) I need to observe changes (after methods like add, removeAt etc.) on my MutableList above but can't achieve…
4
votes
3 answers

why flow collect call more than twice in kotlin?

Hey I am working in kotlin flow in android. I noticed that my kotlin flow collectLatest is calling twice and sometimes even more. I tried this answer but it didn't work for me. I printed the log inside my collectLatest function it print the log. I…
4
votes
1 answer

StateFlow: Cancellation of Older Emitted State After Collecting

I m relatively new in kotlin flows and I m creating the Login Module using Flows in android. I have been stuck from past few days in flows as I m collecting it in ViewModels but I m facing problem when requesting with wrong Credentials its caching…
waheed shah
  • 494
  • 7
  • 19
4
votes
0 answers

How to map StateFlow values and pass it to view via databinding

I have a StateFlow that I want to use with Android data binding. As values in the flow I use a sealed type: data class State( val idState: IdState ) : UiState sealed class IdState { object NotAssigned : IdState() data class Assigned(val…
Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
4
votes
1 answer

Problem with Jetpack Compose Navigation and StateFlow

The Composable function Application creates a NavHostController which defines 2 targets. A StartScreen and a ContentScreen. The StartScreen has just a single button, which triggers a simulated backend request and changes State (using kotlins…
1
2
3
12 13