question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flow.last() operator

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
fvascocommented, Sep 14, 2020

Hi, @gildor, you need a Flow with a state, strictly speaking on this you case, you should prefer a Deferred (or a CompletableStateFlow, which looks very ugly).

I am not suggesting you to use a Deferred instead of Flow, I am considering that a deferred task should put its result in a Deferred.

1reaction
fvascocommented, Dec 1, 2020

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 to Flow, but last is missing.

Here our patch:

public suspend fun <T> Flow<T>.last(): T? = reduce { _, value -> value }
public suspend fun <T> Flow<T>.lastOrNull(): T? = fold<T?, T?>(null) { _, value -> value }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found