gpt4 book ai didi

android - 房间数据库 : Still getting "no value passed for parameter ' id' "even though it is meant to be autogenerated

转载 作者:行者123 更新时间:2023-12-02 12:08:56 32 4
gpt4 key购买 nike

这是我的房间实体:

@Entity(tableName = "matched_users")
data class MatchedUser(
@PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = "match_id") val matchId: String
)

这是我在我的 fragment 中实例化它:
private fun pass(){
CoroutineScope(coroutineContext).launch {
val match = MatchedUser()
CustomApplication.database?.matchedUsersDao()?.addMatchUid(match)
Log.d(TAG, "Added matchId to DB")
}
return removeUser2()
}

当我将鼠标悬停在 MatchedUser() 上时它仍然说我需要为 id 传递一个参数..但它是按照实体中的说明自动生成的。

知道为什么吗?

最佳答案

kotlin数据类中的每个变量都应该被初始化,因此您可以在数据类构造函数中设置默认参数,如下所示:

@Entity(tableName = "matched_users")
data class MatchedUser(
@PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = "match_id") val matchId: String
){
constructor(matchId: String): this(Int.MIN_VALUE, matchId)
}

现在您可以通过仅提供 match_id 来插入数据到 constructor的数据类,像这样:
private fun pass(){
CoroutineScope(coroutineContext).launch {
val match = MatchedUser("1")
CustomApplication.database?.matchedUsersDao()?.addMatchUid(match)
Log.d(TAG, "Added matchId to DB")
}
return removeUser2()
}

关于android - 房间数据库 : Still getting "no value passed for parameter ' id' "even though it is meant to be autogenerated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646020/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com