Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asStateFlow vs simple type cast documentation #3114

Closed
ggajews opened this issue Dec 29, 2021 · 2 comments
Closed

asStateFlow vs simple type cast documentation #3114

ggajews opened this issue Dec 29, 2021 · 2 comments

Comments

@ggajews
Copy link

ggajews commented Dec 29, 2021

Hi guys.
Kotlin guide recommends using asStateFlow to return read only StateFlow

private val _counter = MutableStateFlow(0) // private mutable state flow
val counter = _counter.asStateFlow() // publicly exposed as read-only state flow

Android guide is using simple type cast though:

// Backing property to avoid state updates from other classes
private val _uiState = MutableStateFlow(LatestNewsUiState.Success(emptyList()))
// The UI collects from this StateFlow to get its state updates
val uiState: StateFlow<LatestNewsUiState> = _uiState

I cannot find a good answer in the documentation what are the differences between these two approaches? What is added value for using the asStateFlow() ?

@fvasco
Copy link
Contributor

fvasco commented Dec 29, 2021

In your example uiState is MutableStateFlow, instead a counter !is MutableStateFlow
See also #1973

@ggajews
Copy link
Author

ggajews commented Dec 29, 2021

ok so it's "safer" to use asStateFlow(), I'll create an issue in Android repo to align the sample

@ggajews ggajews closed this as completed Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants