Skip to content

Commit 770ffa8

Browse files
committed
Android Crash: [Not fixed in 5.230.0] java.util.NoSuchElementException - kotlinx.coroutines.flow.FlowKt__ReduceKt.first(Reduce.kt:92) (#5932)
Task/Issue URL: https://app.asana.com/1/137249556945/project/1202552961248957/task/1210012823537208?focus=true ### Description This PR replaces usages of `first()` with `firstOrNull()` in two locations to prevent potential crashes when collecting from empty flows. ### Steps to test this PR N/A
1 parent 12170fb commit 770ffa8

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/ui/credential/management/AutofillSettingsViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import kotlinx.coroutines.flow.StateFlow
117117
import kotlinx.coroutines.flow.combine
118118
import kotlinx.coroutines.flow.distinctUntilChanged
119119
import kotlinx.coroutines.flow.first
120+
import kotlinx.coroutines.flow.firstOrNull
120121
import kotlinx.coroutines.launch
121122
import kotlinx.coroutines.withContext
122123
import timber.log.Timber
@@ -670,7 +671,7 @@ class AutofillSettingsViewModel @Inject constructor(
670671
Timber.v("Opened autofill management screen from from %s", launchSource)
671672

672673
val source = launchSource.asString()
673-
val hasCredentialsSaved = autofillStore.getCredentialCount().first() > 0
674+
val hasCredentialsSaved = (autofillStore.getCredentialCount().firstOrNull() ?: 0) > 0
674675
pixel.fire(AUTOFILL_MANAGEMENT_SCREEN_OPENED, mapOf("source" to source, "has_credentials_saved" to hasCredentialsSaved.toBinaryString()))
675676
}
676677
}

broken-site/broken-site-impl/src/main/java/com/duckduckgo/brokensite/impl/BrokenSitePomptDataStore.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.flow.Flow
4343
import kotlinx.coroutines.flow.distinctUntilChanged
4444
import kotlinx.coroutines.flow.first
45+
import kotlinx.coroutines.flow.firstOrNull
4546
import kotlinx.coroutines.flow.map
4647

4748
interface BrokenSitePromptDataStore {
@@ -149,7 +150,7 @@ class SharedPreferencesDuckPlayerDataStore @Inject constructor(
149150
): Int {
150151
val allDismissEvents = store.data.map { prefs ->
151152
prefs[DISMISS_EVENTS]?.toSet() ?: emptySet()
152-
}.first()
153+
}.firstOrNull() ?: emptySet()
153154

154155
return allDismissEvents.count { dateString: String ->
155156
try {

0 commit comments

Comments
 (0)