Flow.last() operator
See original GitHub issueI found myself often doing this flow.toList().last()
usually for code that works with Flow which represents progress, when I want to receive the final item, it would be complimentary to first()/single() item
It also can be replaced with something like
flow
.filterInInstance<SomeTerminalItemType>()
.single()
but it’s not always so simple, for example, a sealed hierarchy may have a few terminal types for operation
Reactive libraries have such operator http://reactivex.io/documentation/operators/last.html
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:10 (9 by maintainers)
Top Results From Across the Web
last - Kotlin
The terminal operator that returns the last element emitted by the flow. Throws NoSuchElementException if the flow was empty.
Read more >Kotlin Coroutines Flow in a nutshell - ProAndroidDev
The collecting process goes from the bottom up and starts immediately after the terminal operator call. Terminal operators in flow: collect(); first(); toList()...
Read more >Kotlin flows on Android - Android Developers
In coroutines, a flow is a type that can emit multiple values sequentially, as opposed to suspend functions that return only a single...
Read more >Android — 9 Useful Kotlin Flow Operators You Need to Know
Similar to debounce , it's also used to filter items from the Flow but it has one important difference — Instead of checking...
Read more >When to use collect and collectLatest operator to collect kotlin ...
The crucial difference from collect is that when the original flow emits a new value then the action block for the previous value...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, @gildor, you need a
Flow
with a state, strictly speaking on this you case, you should prefer aDeferred
(or aCompletableStateFlow
, which looks very ugly).I am not suggesting you to use a
Deferred
instead ofFlow
, I am considering that a deferred task should put its result in aDeferred
.We got the same issue, we elaborate a sequence of event and have to get the last one.
Unfortunately, we require a suspend fun inside a
filter { }
, so we have to switch toFlow
, butlast
is missing.Here our patch: