flamy
07/16/2024, 1:15 PMflamy
07/16/2024, 1:15 PMsealed interface UiState<out T> {
data class Success<T>(val value: T) : UiState<T>
data class Error(val exception: Throwable? = null, val message: String = ""): UiState<Nothing>
data object Loading : UiState<Nothing>
}
val currentPathFiles: StateFlow<UiState<List<File>>> = currentPath
.filterNotNull()
.flowOn(<http://dispatcher.io|dispatcher.io>())
.flatMapLatest { directoryProvider.observeDir(it) }
.map { UiState.Success(it) }
.catch { emit(UiState.Error(it)) }
.toStateFlow(initialState = UiState.Loading)
Dmitry Khalanskiy [JB]
07/16/2024, 1:20 PM.map { UiState.Success(it) as UiState<List<File>> }
Otherwise, map
thinks that the new flow type must be UiState.Success
, as that's the most specific type it can infer. You can let it know that you want a less specific type.flamy
07/16/2024, 1:25 PMChrimaeon
07/16/2024, 3:09 PMit
in catch
is a exception so you are not emitting a UIState with list as declared in the return typeRok Oblak
07/16/2024, 3:14 PMephemient
07/17/2024, 5:25 AM.map<_, UiState<List<File>>> { UiState.Success(it) }
.catch { emit(UiState.Error(it)) }
or
.map { UiState.Success(it) }
.catch<UiState<List<File>>> { UiState.Error(it) }
which avoids as