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
2
votes
3 answers

Compose LazyColumn key, messes up scrolling when sorting the items

I'm trying to implement simple sort with items that are in a state. This will work perfectly, only problem is that the animation is now gone, because it doesn't have a key. LazyColumn( state = listState, ) { items(items.size) { index -> …
2
votes
1 answer

Can I cancel my stateFlow respective collection using the stateFlow instance itself?

I can launch my stateFlow collection as below val collectingScope = CoroutineScope(Dispatchers.Default) val stateFlow = MutableStateFlow(0) val myJob = collectingScope.launch { stateFlow.collect { println("collected $it") …
Elye
  • 53,639
  • 54
  • 212
  • 474
2
votes
1 answer

How to get list flow from var list or a mutable list

I want a flow that'll emit a Flow> when the list updates/changes. Case 1: var l = listOf(1, 2, 3, 5) val listFlowOfInt : Flow> = // A flow which will notify All subscribers (emit value) when l is re-assigned, //say l=…
Cyber Avater
  • 1,494
  • 2
  • 9
  • 25
2
votes
3 answers

Jetpack Compose passing LiveData to a Composable lambda

I'm having trouble understanding why my value I'm observing as state is properly passed into a Composable lambda, but does not trigger that composable to recompose. Here's my setup. Screen @Composable fun Screen( showBottomSheet: (@Composable ()…
Saehun Sean Oh
  • 2,103
  • 1
  • 21
  • 41
2
votes
1 answer

StateFlow collect not firing for list type

@HiltViewModel class HistoryViewModel @Inject constructor(private val firebaseRepository: FirebaseRepository) : ViewModel() { private val translateList: MutableList = mutableListOf() private val _translateListState:…
2
votes
1 answer

How to send Kotlin Flow events into existing Flow stream via combine?

I have two flows. flow1() emits a stream of integers every second. flow2() emits an integer when a button is clicked. Both flows are combined with combinedFlows() and the combined results are collected and ouput in the textView. Problem: The…
mars8
  • 770
  • 1
  • 11
  • 25
2
votes
1 answer

How to make an emit from another function in a flow after flow.stateIn()?

I get page data from a database, I have a repository that returns a flow. class RepositoryImpl (private val db: AppDatabase) : Repository { override fun fetchData (page: Int) = flow { emit(db.getData(page)) } } In the ViewModel, I…
2
votes
2 answers

Kotlin Flow - Some emitted events not received when collect

I am using MutableStateFlow. My flow type is sealed class with different states (Loading, Success, Error, etc). Initial value of my flow is empty: private val _updateDepartmentsState =…
2
votes
0 answers

MutableStateFlow is not updating value

I have problem working with MutableStateFlow, I cannot understand how it is working or I am mistaken somewhere. For example purpose I created simpler classes to get the idea what I am doing. First I have data class which holds the values and…
2
votes
2 answers

Firestore with StateFlow real time change is observed in repo and view model, but adapter is not being updated

As a developer one needs to adapt to change, I read somewhere it says: If you don’t choose the right architecture for your Android project, you will have a hard time maintaining it as your codebase grows and your team expands. I wanted to…
2
votes
1 answer

Converting livedata to stateflow

Android, Kotlin I have the following livedata in my datasource class, I cannot change this to StateFlow, so need to convert it to StateFlow in my viewModel val trackingCatalogInitialLoadLiveData: LiveData> by lazy { …
ant2009
  • 27,094
  • 154
  • 411
  • 609
2
votes
1 answer

State flow Android Kotlin

I have a god view model for every thing I know this is wrong but I am just experimenting with Flow I have these two State flow variables in view model private val _currentRestroMenu = MutableStateFlow>(State.loading()) private val…
Rahul Siriya
  • 37
  • 1
  • 1
  • 6
2
votes
1 answer

stateflow refershing whole data in reyclerview android kotlin

Hey I am learning stateflow in android kotlin. I am creating a reverse conversation calendar view with the help of reyclerview. In my mainactivity there is one fragment, and inside that I have reyclerview. My goal to is doing paging stuff in my…
2
votes
1 answer

Is there a way to create a StateFlow from a Flow

Can I create a StateFlow from a Flow so that I can get the .value of it? Is there a way to do it without using .collect? Something like val myStateFlow = StateFlow(this.existingFlow) So that later, when someone clicks a button, I can…
2
votes
1 answer

Is LiveData hot or cold?

We know that StateFlow and SharedFlow are hot. StateFlow is a hot flow—it remains in memory as long as the flow is collected or while any other references to it exist from a garbage collection root. SharedFlow is a hot flow that emits values to…