Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 836
Ali Avatar asked Dec 24 '18 21:12

Ali


People also ask

Are getters and setters necessary in Kotlin?

In programming, getters are used for getting value of the property. Similarly, setters are used for setting value of the property. In Kotlin, getters and setters are optional and are auto-generated if you do not create them in your program.

Does Kotlin generate getters and setters?

A var in Kotlin already generates a getter and setter.

How do I add getter and setter to Kotlin?

In Kotlin, getter() and setter() methods need not be created explicitly. The Kotlin library provides both of them by default.


1 Answers

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.

like image 113
CommonsWare Avatar answered Sep 21 '22 03:09

CommonsWare