In my ViewModel in Android project:
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
class MainViewModel(application: Application) : AndroidViewModel(application) {
private var waitressCallContainerHeights = MutableLiveData<Pair<Int, Int>>()
fun init() {
waitressCallContainerHeights.value.first = 200
}
}
But I get compile error in this line:
waitressCallContainerHeights.value.first = 200
Val cannot be reassigned
I want to set first and second values in fun init
Although your liveData is mutable, the Pair itself isn't. You need to do:
waitressCallContainerHeights.value = Pair(200,300/* for example */)
You can also initialise the variable on the declaration itself with apply, something like this:
private var waitressCallContainerHeights = MutableLiveData<Pair<Int, Int>>().apply { value = Pair(200, 300) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With