Questions tagged [kotlin-flow]

In Kotlin coroutines, a Flow is a stream of asynchronously computed values. Use this tag when you're having problems building, transforming or consuming flows.

In Kotlin coroutines, a Flow is stream of asynchronously computed values.

Documentation: https://kotlinlang.org/docs/flow.html#flows

895 questions
96
votes
5 answers

Kotlin Flow vs Android LiveData

I have some questions about Kotlin Flow I can observe LiveData from multiple Fragments. Can I do this with Flow? If yes then how? We can have multiple LiveData from a single LiveData using map& switchMap. Is there any way to have multiple Flow from…
zoha131
  • 1,758
  • 2
  • 16
  • 18
73
votes
7 answers

Is Kotlin Flow's Collect is only internal kotlinx.coroutines API?

Taking the direct example from https://kotlinlang.org/docs/reference/coroutines/flow.html#flows-are-cold fun simple(): Flow = flow { println("Flow started") for (i in 1..3) { delay(100) emit(i) } } fun main() =…
Elye
  • 53,639
  • 54
  • 212
  • 474
42
votes
3 answers

Type mismatch inferred type is () -> Unit but FlowCollector was expected

When trying to collect from a Flow the type suddenly mismatches, and it was working and then started suddenly. In my viewmodel: class MyViewModel: ViewModel() { lateinit var river: Flow fun doStuff() { river = flow { …
just_user
  • 11,769
  • 19
  • 90
  • 135
34
votes
2 answers

When to use collect and collectLatest operator to collect kotlin flow?

I want to know a practical scenario of both of them. I know the difference but couldn't relate to my implementation.
Jahid Hasan
  • 439
  • 1
  • 4
  • 4
34
votes
8 answers

Unit test the new Kotlin coroutine StateFlow

Recently, the class StateFlow was introduced as part of Kotlin coroutines. I'm currently trying it and encountered an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values…
agonist_
  • 4,890
  • 6
  • 32
  • 55
29
votes
5 answers

How to emit Flow value from different function? Kotlin Coroutines

I have a flow : val myflow = kotlinx.coroutines.flow.flow{} and want to emit values with function: override suspend fun sendMessage(chat: Chat, message: Message) { myflow.emit(message) } But compiler does not allow me to do this, is…
user13475465
27
votes
4 answers

LiveData Vs StateFlow: Should we switch from Live data to State Flow?

I have come across articles that recommend switching to StateFlow. Like the one here. Also in the new Android studio, StateFlow support is automatically included in the functionality of data binding, including the coroutines dependencies. Live data…
26
votes
2 answers

Android Kotlin Coroutines: what is the difference between flow, callbackFlow, channelFlow,... other flow constructors

I have code that should change SharedPreferences into obsarvable storage with flow so I've code like this internal val onKeyValueChange: Flow = channelFlow { val callback = SharedPreferences.OnSharedPreferenceChangeListener { _, key…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
23
votes
5 answers

How to get the value of a Flow outside a coroutine?

How can I get the value of a Flow outside a coroutine similarly to LiveData? // Suspend function 'first' should be called only from a coroutine or another suspend function flowOf(1).first() // value is null flowOf(1).asLiveData().value //…
maxbeaudoin
  • 6,546
  • 5
  • 38
  • 53
21
votes
5 answers

Cannot resolve symbol repeatOnLifecycle in Android

I'm following this article to collect flows in UI. But I couldn't resolve repeatOnLifeCycle API in my code. I have added the below dependency, though. lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03 Please help
Rakesh
  • 1,205
  • 1
  • 14
  • 33
21
votes
5 answers

Kotlin Flow returned from Room does not update when an insert is performed from another Fragment/ViewModel

I have a Room database that returns a Flow of objects. When I insert a new item into the database, the Flow's collect function only triggers if the insert was performed from the same Fragment/ViewModel. I have recorded a quick video showcasing the…
Marc
  • 310
  • 1
  • 3
  • 6
19
votes
8 answers

Getting kotlin error "After waiting for 60000 ms, the test coroutine is not completing"

I'm new at testing, trying to take second flow value and assert it, When i run this test one by one runs fine but when i run whole test once first test runs fine and rest of test give me timeout error. Error : After waiting for 60000 ms, the test…
Jonas
  • 207
  • 2
  • 7
19
votes
1 answer

No type arguments expected for class Flow

I encountered a problem with Kotlin Flow. I copied the following code code from the Official guide fun simple(): Flow = flow { for (i in 1..3) { delay(100) emit(i) } } But the Android Studio prompts a following…
巴赫哥德尔
  • 193
  • 1
  • 4
17
votes
3 answers

Combine multiple Kotlin flows in a list without waiting for a first value

I have a List>, and would like to generate a Flow>. This is almost what combine does - except that combine waits for each and every Flow to emit an initial value, which is not what I want. Take this code for example: val a = flow { …
Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
17
votes
4 answers

How can I debounce a setOnClickListener for 1 second using Kotlin Coroutines?

When user taps fast on the button the showDialog() method displays multiple times on top of each other, so when you dismiss it there is another one behind it. I am looking for a way to ignore the second tap for 1 second without using a handler or…
1
2 3
59 60