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.
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.
A var in Kotlin already generates a getter and setter.
In Kotlin, getter() and setter() methods need not be created explicitly. The Kotlin library provides both of them by default.
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.
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