I understand the need for creating getter and setter points for LiveData in the ViewModel. However, I am looking to understand how the get() syntax works in Kotlin.


val isRealtime: LiveData<Boolean>
get() = _isRealtime
private val _isRealtime = MutableLiveData<Boolean>()`

Here is a good explanation on Stackoverflow: How Does Android LiveData get() syntax work?

get() is not related to Android.

val isRealtime: LiveData<Boolean>
get() = _isRealtime

Here, get() is overriding the automatically-generated Kotlin getter function for the isRealtime property. So, instead of returning its own value, it returns the value of _isRealtime.
Personally, I recommend simpler syntax:

private val _isRealtime = MutableLiveData<Boolean>()
val isRealtime: LiveData<Boolean> = _isRealtime

The objective of either of these is to keep the mutability private, so consumers of this class do not accidentally update the MutableLiveData themselves.

Creator of Coinverse - The 1st Crypto News Audiocast App @ bit.ly/play-coin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store