Search code examples
androidkotlinandroid-viewmodel

MutableLiveData<Pair<Float, Float>>() -> Val cannot be reassigned


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


Solution

  • Although your liveData is mutable, the Pair itself isn't. You need to do: waitressCallContainerHeights.value = Pair(200,300/* for example */)