Is there a way to hook into the setter of a Mutabl...
# compose
r
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.)
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
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.
🙏 1