Is there a way to hook into the setter of a MutableState property of my ViewModel? When it changes I want to persist it, and clear other cached data in the ViewModel. (Code in thread.)
robnik
04/29/2021, 3:00 PM
Copy code
class MyViewModel: ViewModel() {
private val repo = Repository()
var vehicleID: String by mutableStateOf("")
set() { // Compiler does not allow this
repo.saveVehicleID(vehicleID)
cachedInfo = null
}
}
a
Adam Powell
04/29/2021, 3:21 PM
You can use a
private var _vehicleId by mutableStateOf("")
and keep a custom getter and setter for the public property that read and write it. You don't have to do anything special to keep the observability working.