all

suspend fun <T> Flow<T>.all(predicate: suspend (T) -> Boolean): Boolean(source)

A terminal operator that returns true if all elements match the given predicate, or returns false and cancels the flow as soon as the first element not matching the predicate is encountered.

If the flow terminates without emitting any elements, the function returns true because there are no elements in it that do not match the predicate. See a more detailed explanation of this logic concept in the "Vacuous truth" article.

Equivalent to !any { !predicate(it) } (see Flow.any) and none { !predicate(it) } (see Flow.none).

Example:

val myFlow = flow {
repeat(10) {
emit(it)
}
throw RuntimeException("You still didn't find the required number? I gave you ten!")
}
println(myFlow.all { it <= 5 }) // false
println(flowOf(1, 2, 3).all { it <= 5 }) // true

See also