Kurt Renzo Acosta
08/03/2021, 8:02 PMFlow<T>
then I just observe it and wait for the initial value before I update the UI. Now, I’m trying to consume it and it always needs an initial value. I tried to do .collectAsState(initial = null)
but then it makes it nullable and I have to perform null checking on every flow. Is there a better practice around this? I’d like to have Flow<T>
rather than Flow<T?>
. Thanks in advance!Dominaezzz
08/03/2021, 8:08 PMDominaezzz
08/03/2021, 8:09 PMstateIn
in a view model or something, to wait for the first value.Michal Klimczak
08/03/2021, 8:22 PMKurt Renzo Acosta
08/03/2021, 8:23 PMstateIn
is that it’s a suspend function. 😕 so I can’t do a
private val _myFlow = MutableStateFlow<T?>(null)
val myFlow get() = _myFlow.filterNotNull().stateIn(scope)
Kurt Renzo Acosta
08/03/2021, 8:25 PMZach Klippenstein (he/him) [MOD]
08/03/2021, 8:26 PMMichal Klimczak
08/03/2021, 8:26 PMKurt Renzo Acosta
08/03/2021, 8:28 PMeygraber
08/03/2021, 8:29 PMsealed interface StateWrapper {
object Initial : StateWrapper
data class State(val actualState: State) : StateWrapper
}
Then you can do collectAsState(initial = StateWrapper.Initial
and react to that by not rendering, showing skeletons, etc...Kurt Renzo Acosta
08/03/2021, 8:30 PMeygraber
08/03/2021, 8:30 PMursus
08/04/2021, 1:05 AMDominaezzz
08/04/2021, 6:43 AMKurt Renzo Acosta
08/04/2021, 6:47 AMif (state1 != null && state2 != null … && stateN != null)
and I’m trying to avoid that.
And yes, smart cast doesn’t work on delegates so I had to force the null