Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read-only mutableStateListOf

In Jetpack Compose, if I have a MutableState variable, I can expose it’s State as "read-only" value to other classes as State<String>, just like:

private val _title = mutableStateOf("abc")
val title: State<String> = _title

Is there a way to do this with SnapshotStateList<> too? How would I do this for example with:

private val _titles = mutableStateListOf<String>(...)
val titles: ??? = _titles

I know that I could work around this by just using MutableState<List<String>>, but I’d have to provide a whole new list every time I would want to add/remove items.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

mutableStateListOf create an object of SnapshotStateList, which is subclass of MutableList, so you can use List:

val titles: List<String> = _titles

Also with mutableStateOf you can use a single variable with delegation:

var title by mutableStateOf("abc")
    private set
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading