Add asStateFlow to the StateFlow variables in ViewModel class #518
Replies: 6 comments 4 replies
-
You mean, currently we define it like this, right? val uiModels: StateFlow<List<UiModel>> = _uiModels |
Beta Was this translation helpful? Give feedback.
-
Agreed. The Kotlin doc also has an example code like this. However, we should update the description to be correct as @AVI5HEK mentioned, @kaungkhantsoe . |
Beta Was this translation helpful? Give feedback.
-
Agreed and Voted 🚀 |
Beta Was this translation helpful? Give feedback.
-
Agreed and voted ✋ |
Beta Was this translation helpful? Give feedback.
-
Why
Currently, we define the
StateFlow
variables inside theViewModel
as follows:One issue with this approach is we can still convert the
StateFlow
variable to aMutableStateFlow
in the other classesWe can ' emit ' new values once we can cast them to
MutableStateFlow
which we should not allow.If we were implementing an SDK, this could be an issue. One way to restrict such conversion would be using
asStateFlow
extension. If we look into the definition ofasStateFlow
, we will see the following code 👇That way we can restrict the conversion of
StateFlow
toMutableFlow
by defining the variables in ViewModel like the following:Who benefits?
Android developers
7 votes ·
Beta Was this translation helpful? Give feedback.
All reactions