Meyben
Meyben

Reputation: 581

Convert MutableLiveData to MutableState

I am new to jetpack compose. I have this value which is a MutableLiveData. I want ofc to observe on the object and get the value. I was wondering if i could convert the MutableLiveData to MutableState in an easy way? To make it even simpler, or is it best to keep it as a MutableLiveData?

Upvotes: 7

Views: 3759

Answers (1)

Abhimanyu
Abhimanyu

Reputation: 14767

Use observeAsState() to convert LiveData to State.
Use LiveData in ViewModel, Repository and other layers.
Convert and observe it using observeAsState() in the Composable.

Example code

val itemState by viewModel.itemsLiveData.observeAsState()

Source

Upvotes: 9

Related Questions