簡體   English   中英

不確定如何在 Room Android 中將 Cursor 轉換為該方法的返回類型?

[英]Not sure how to convert a Cursor to this method's return type in Room Android?

構建應用程序時出現以下錯誤

Not sure how to convert a Cursor to this method's return type (kotlinx.coroutines.flow.Flow<? extends java.util.List<com.notification.NotificationEntity>>)

這是我的實體類

@Entity
internal data class NotificationEntity(
@PrimaryKey @ColumnInfo(name = "notification_id") val notificationId: String,
val title: String,
val body: String?,
@ColumnInfo(name = "is_actionable") val isActionable: Boolean,
@ColumnInfo(name = "created_at") val createdAt: Instant,
@ColumnInfo(name = "is_read") val isRead: Boolean = false)

這是我的道課

@Dao
internal interface NotificationDao {

@Query("SELECT * FROM NotificationEntity ORDER BY created_at ASC")
suspend fun getAllNotifications(): Flow<List<NotificationEntity>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveNotification(notifications: List<NotificationEntity>)
}

有人可以幫助這里有什么問題嗎?

當您聲明返回Flow的 Dao 函數時,該函數不應是可掛起的。 請參閱文檔。

將您的代碼更改為:

@Dao
internal interface NotificationDao {

@Query("SELECT * FROM NotificationEntity ORDER BY created_at ASC")
fun getAllNotifications(): Flow<List<NotificationEntity>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveNotification(notifications: List<NotificationEntity>)
}

如果尚未添加,則為 NotificationEntity 中使用的Instant類型添加類型轉換器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM