我的实体类:
@Entity(tableName = "student")
data class Student(
    var name: String,  
    var age: Int,   
    var gpa: Double,
    var isSingle: Boolean,
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0,    
    @Ignore                           //don't create column in database, just for run time use
    var isSelected: Boolean = false                     
)
Run Code Online (Sandbox Code Playgroud)
然后我像这样插入(在 中测试androidTest):
val student = Student("Sam", 27, 3.5, true)
studentDao.insert(student)
Run Code Online (Sandbox Code Playgroud)
添加@Ignore注释后,它立即给了我这个错误:
C:\Android Project\RoomTest\app\build\tmp\kapt3\stubs\debug\com\example\roomtest\database\Student.java:7: ????: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor …Run Code Online (Sandbox Code Playgroud) 我正在使用房间数据库和数据存储。这是我的代码
视图模型
@HiltViewModel
class SubscriptionViewModel @Inject constructor(
    private val userDataStore: UserDataStore,
    private val walletDao: WalletDao
) : ViewModel() {
val flow: Flow<List<Transaction>> = walletDao.getAllTransactions(userDataStore.getId()) 
 // This shows an error as userDataStore.getId() is a suspend function in DataStore
 init {
        viewModelScope.launch(IO) {
            getWalletAmount()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
数据存储
 suspend fun getId(): String {
        val preferences = dataStore.data.first()
        return preferences[USER_ID] ?: ""
    }
Run Code Online (Sandbox Code Playgroud)
钱包道
 @Query("SELECT * FROM recent_transactions where user_id=:user_id")
    fun getAllTransactions(user_id: String): Flow<List<Transaction>>
Run Code Online (Sandbox Code Playgroud)
问题是我无法初始化流程。我尝试使用 Lateinit 在 init{ } 块中初始化它,但是在片段中访问时它崩溃,表示流程尚未初始化。
任何解决方案都会有帮助。
只是测试Preferences DataStore并发现提供的Flow输出不会发出相同的值,我的设置如下:
数据存储实用程序类:
object DataStore {
    private val Context.settings by preferencesDataStore("settings") 
    suspend fun saveBoolean(context: Context, keyResId: Int, value: Boolean) {
        val key = booleanPreferencesKey(context.getString(keyResId))
        context.settings.edit {
            it[key] = value
        }
    }
    fun getBooleanFlow(context: Context, keyResId: Int, defaultValueResId: Int): Flow<Boolean> {
        val key = booleanPreferencesKey(context.getString(keyResId))
        val defaultValue = context.resources.getBoolean(defaultValueResId)
        return context.settings.data.map {
            it[key] ?: defaultValue
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
视图模型类:
class FirstViewModel(application: Application) : AndroidViewModel(application) {
    private val uiScope = viewModelScope
    val isUpdateAvailable = DataStore.getBooleanFlow(
        getApplication(), R.string.is_update_available_key, R.bool.is_update_available_default …Run Code Online (Sandbox Code Playgroud) android android-preferences sharedpreferences kotlin-flow android-jetpack-datastore
我已经阅读了 kotlin 文档并尝试了BooleanArray.count(predicate: (Boolean) -> Boolean),但它仍然返回 的大小,而array不是数量true:
val checks = arrayOf(true, false, true, true, true)
val trueCount = checks.count { true }
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题?