Search code examples
androidkotlinandroid-viewmodel

property getter or setter expected in Kotlin ViewModel class


I have following ViewModel class :

class PersonViewModel(
        context: Application,
        private val dataSource: MoviesRemoteDataSource)
    : AndroidViewModel(context) {

    internal val compositeDisposable = CompositeDisposable()
    val person: ObservableField<Person>()
    private val isVisible = ObservableBoolean(false)

    fun showPerson(personId: String) {
        val personSubscription = dataSource.getPerson(personId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ person ->
                    isVisible.set(true)
                    this.person.set(person)
                }
                ) { throwable -> Timber.e(throwable) }

        compositeDisposable.add(personSubscription)
    }
}

And this is Person class :

class Person(
        @SerializedName("birthday")
        var birthDay: String?,
        @SerializedName("deathday")
        var deathDay: String?,
        var id: Int,
        @SerializedName("also_known_as")
        var alsoKnowAs: List<String>,
        var biography: String,
        @SerializedName("place_of_birth")
        var placeOfBirth: String?)

It shows an error on this line in ViewModel:

val person: ObservableField<Person>()

It says : property getter or setter expected

I appreciate for your help.


Solution

  • Most likely, replace:

    val person: ObservableField<Person>()
    

    with:

    val person = ObservableField<Person>()
    

    This sets up person to be initialized with the ObservableField<Person> that you are creating.