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

How to inject viewModelScope for Android unit test with Kotlin coroutines?

Questions What is the best strategy to inject viewModelScope for Android unit tests with Kotlin coroutines? When the CoroutineScope is injected into a ViewModel for unit tests, should the CoroutineDispatcher also be injected and defined using…
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
11
votes
3 answers

How to resume flow after exception

I have the following code: val channel = BroadcastChannel(10) fun setup() { scope.launch { channel.asFlow(). .flatMapLatest { fetchSomeData() } .catch { emit(DefaultData()) } .onEach {…
gookman
  • 2,527
  • 2
  • 20
  • 28
10
votes
1 answer

Kotlin - StateFlow not emitting updates to its collectors

I got a StateFlow of type UserStateModel (data class) in my app. private val _userStateFlow: MutableStateFlow = MutableStateFlow(UserStateModel()) val userStateFlow: StateFlow = _userStateFlow here is the…
10
votes
2 answers

How to convert a Flow to MutableStateFlow?

I am trying to keep a mutable state flow in my class, but when I apply any methods to it, it will be converted to an immutable Flow: class MyClass : Listener { private val source = Source() val flow: Flow
Prem
  • 3,373
  • 7
  • 29
  • 43
10
votes
1 answer

Kotlin state flows not emitting in unit test with view model scope

I'm new to Kotlin Coroutines and Flows and unit testing them. I have a pretty simple test: @Test fun debounce(): Unit = runBlocking { val state = MutableStateFlow("hello") val debouncedState = state.debounce(500).stateIn(this,…
frangulyan
  • 3,580
  • 4
  • 36
  • 64
10
votes
0 answers

DataStore not receiving event from Flow

I am playing with datastore-preferences:1.0.0-alpha01 and I cannot seem to get an event back when the datastore value has been updated. I have been trying to observe it in the fragment and the parent activity with the same result. When I create an…
DevinM
  • 1,112
  • 1
  • 12
  • 29
10
votes
3 answers

Get last item emited by a Flow and dont receive updates

What is the best way to obtain the last element emitted by a flow without receiving updates. Question: I use a flow to observe changes in certain shared preferences, but sometimes I want to know the current value of that preference. I always use two…
cmpeguerog
  • 537
  • 2
  • 7
  • 12
9
votes
1 answer

StateFlow is emitting every time the activity paused and resumed

I have a state flow that emits a value once the HTTP request is returned with the response, the value will be shown as a list in Activity, I'm using Kotlin coroutines StateFlow for communication between the ViewModel and Activity. I'm using androidx…
9
votes
2 answers

Kotlin Flow with catch operator still completes

I am having trouble understanding how the catch operator works in a kotlin Flow. Here is the catch documentation Questions: Why doesn't the presence of a catch allow the Flow to continue upon encountering an exception, rather than…
Greg
  • 93
  • 1
  • 1
  • 4
9
votes
2 answers

Convert InputStream to Flow

I would like to read a file contents and emit a flow for every line of its contents. So, I have to implement a function with the following signature: fun InputStream.linesToFlow(): Flow Is there any way to implement this function?
alisabzevari
  • 8,008
  • 6
  • 43
  • 67
9
votes
3 answers

GroupBy operator for Kotlin Flow

I am trying to switch from RxJava to Kotlin Flow. Flow is really impressive. But Is there any operator similar to RxJava's "GroupBy" in kotlin Flow right now?
Sundaravel
  • 464
  • 5
  • 16
8
votes
2 answers

Get current and previous value in flow's collect

I need to handle current and previous value in flow collect, so I need some operator that acts like that: ----A----------B-------C-----|---> ---(null+A)---(A+B)---(B+C)--|---> One idea is something like: fun Flow.withPrevious():…
8
votes
5 answers

Why functions that produce LiveData or Flow, don't have to be called from CoroutineScope?

When we usually use Room, we use Kotlin Coroutine and make a DAO to access Room and to get the result. most of functions usually have suspend modifier at the beginning of function but LiveData and Flow. for instance, let's take a look these two code…
8
votes
3 answers

Kotlin Flow: unsubscribe from SharedFlow when Fragment becomes invisible

I've read similar topics but couldn't find a proper answer: How to end / close a MutableSharedFlow? Kotlin Flow: How to unsubscribe/stop StateFlow and SharedFlow. Making cold flows hot using shareIn - Android docs Introduce SharedFlow - GH…
adek111
  • 1,026
  • 2
  • 14
  • 29
8
votes
1 answer

Android Paging 3 library: how to change list with new param?

I have a search fragment that shows list of searched items. if user type something, I pass that string to url as new query parameter and get new list using paging 3 library. first solution is: //viewModel lateinit var postListUrl: String val…
Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38
1 2
3
59 60