LiveData 在 Jetpack Compose Android 上返回 null

问题描述 投票:0回答:0

@Query("SELECT COUNT(category) FROM todolist_table")
    fun getAllTaskCount() : LiveData<Int>

回购

 suspend fun getAllTaskCount() : LiveData<Int> {
       return todoDao.getAllTaskCount()
    }

视图模型

  fun getAllTaskCount(){
         viewModelScope.launch(Dispatchers.IO) {
             repository.getAllTaskCount()
         }
    }

查看

  val viewModel = ToDoViewModel(application = LocalContext.current.applicationContext as Application)

  val taskCount = viewModel.getAllTaskCounter()

我想从数据库中计算类别,但它返回 null

android android-jetpack-compose android-room android-livedata android-viewmodel
© www.soinside.com 2019 - 2024. All rights reserved.