Hi, folks! Does anyone know if there is any partic...
# android-architecture
e
Hi, folks! Does anyone know if there is any particular reason why you cannot use a
Flow
with data binding, but it needs to be a
StateFlow
, for one-way binding? As far as my imagination goes, you should be able to just collect a simple
Flow
and update the view from the
FlowCollector
, but I might be missing something. Also, are there any known workarounds? I didn't manage to find anything by Googling. The context is that we are looking to migrate from LiveData to Flows, and we have hundreds, if not thousands of LDs that are bound in XML that now would become Flows, and going through each and one of them to transform them to a
StateFlow
using the
stateIn()
operator is not very feasible 😞
c
I think
StateFlow
is desired over
Flow
because it requires an initial state similar to
LiveData
.
e
m
Yeah, but what does it really mean for you to bind something without a value to UI element? That's the issue with LiveData. When you use StateFlow, you always have a value, even if that value happens to be null (when you use a nullable type parameter), which i don't recommend anyway.
Plus, databinding should support Flow and StateFlow out of the box as far as I know. It has for quite some time.
e
I agree, an advantage of
StateFlow
is that you always have a value. The problem is that sometimes it's harder to have a
StateFlow
at hand. For instance imagine if you need to apply several operations like
map
, the result will be a simple
Flow
because of the suspending nature of the operators the value won't be there right away. You could then do
.stateIn(scope, started, initialValue)
, but when you have a lot of different fields you need to do this for, it becomes annoying, to say the least, or you might not even have a sensible
initialValue
to begin with. What would I expect to happen when using
Flow
would be similar with what happens when using
LiveData
, which is for nothing to be set on the view until a value is emitted 🤷