Writing good code between Dao - Repository - ViewModel. The function always return null value.
from Entity:
@Entity(tableName = "diabete")
class Diabete(
@PrimaryKey(autoGenerate = true)
@NonNull
@ColumnInfo(name = "diabeteId")
var id: Int,
var date: LocalDateTime,
var balise: String,
var taux: Float,
var note: String
)
from Dao:
// Obtenir la moyenne du taux selon la balise
@Query("SELECT IFNULL(avg(taux), 0.0) FROM diabete WHERE balise = :balise")
fun avgByBalise(balise: String): LiveData<Float>
from Repository:
fun avgBaliseAJeun(balise: String): LiveData<Float> {
return dbDao.avgByBalise(balise)
}
from ViewModel:
fun avgBaliseAJeune(balise: String): LiveData<Float> {
val result = MutableLiveData<Float>()
viewModelScope.launch(Dispatchers.IO) {
val retour = repository.avgBaliseAJeun(balise)
result.postValue(retour.value)
}
return result
}
from Fragment:
val avgBaliseAJeun: Float = dpViewModel.avgBaliseAJeune("À jeun").observeAsState(initial = 0F).value
This line always return null when debugging.
All the compilation is ok.
The application crash when running.
What is missing?