46

I'm trying to implement a DB Observer with LiveData as described in the android documentation.

As long as I'm programming in Kotlin I'm adapting the functions (originally written in Java) to it.

When trying to save the data I find this problem.

Cannot assign to ‘value’: the setter is protected/*protected and package*/ for synthetic extension in ‘<library Grade: android.arch.livecycle:livedata-core-1.1.1>’

Did anyone have this problem already?

This is my code:

ViewModel:

class ProfileViewModel: ViewModel() {

    object FirstName: MutableLiveData<String>()

    fun getCurrentName(): LiveData<String> {
        return FirstName
    }
}

Fragment

class ProfileFragment{

    private lateinit var model: ProfileViewModel

    // this is called onViewCreated. inputFirstName is an Edittext.
    override fun setUp() {
        model = ViewModelProviders.of(this).get(ProfileViewModel::class.java)

        val nameObserver = Observer<String> { firstName ->
            inputFirstName.text = SpannableStringBuilder(firstName)
        }

        model.getCurrentName().observe(this, nameObserver)
    }

    fun saveProfileData() {
        val firstName = inputFirstName.text.toString()
        model.getCurrentName().value = firstName
    }
}
Sagar
  • 23,903
  • 4
  • 62
  • 62
kike
  • 4,255
  • 5
  • 23
  • 41
  • 6
    replace `fun getCurrentName(): LiveData` with `fun getCurrentName(): MutableLiveData` – pskink Jun 19 '18 at 07:58
  • Yes, that was the problem. Would you elaborate it a bit more on an answer so we can keep it for the community? – kike Jun 19 '18 at 09:04
  • 2
    see the difference in `LiveData#setValue()` and `MutableLiveData#setValue()` methods – pskink Jun 19 '18 at 09:11

2 Answers2

65

As @spkink suggested:

replace

fun getCurrentName(): LiveData<String>

with

fun getCurrentName(): MutableLiveData<String>

The error is caused because setValue(T value) is protected in LiveData (so you cannot call it) while it is public in MutableLiveData.

Hanzala
  • 1,965
  • 1
  • 16
  • 43
RPG
  • 666
  • 7
  • 5
1
model.getCurrentName().value = firstName

Please consider that handling the name value update inside the ViewModel might be a clearer solution, since you avoid having mutable public properties. Furthermore, here it could lead to even worse effects because you have two mutable public live data properties. For example:

ViewModel

// keep your MutableLiveData private, so it's easier to find where mutations might happen in your code
// I changed a little how the variable is declared, as the way below seems to more common nowadays
private var _firstName = MutableLiveData<String>()

// LiveData should remain public, being a read-only interface with other classes like your fragment
fun getCurrentName(): LiveData<String> {
    return _firstName
}

/*
An alternative 
val firstName = LiveData<String>
    get() = _firstName
*/

fun setFirstName(firstName: String){
    _firstName.value = firstName
}

Fragment

fun saveProfileData() {
    val firstName = inputFirstName.text.toString()
    // the value update will be handled inside the ViewModel, where the private mutable live data property is located
    model.setFirstName(firstName)
}
alereca
  • 76
  • 1
  • 4