【问题标题】:android datastore-preferences: Property delegate must have a 'getValue(Context, KProperty<*>)' methodandroid datastore-preferences:属性委托必须有一个 'getValue(Context, KProperty<*>)' 方法
【发布时间】:2022-02-28 20:19:28
【问题描述】:

我正在编写一个 jetpack compose android 应用程序,我需要永久存储一些设置。

我决定使用androidx.datastore:datastore-preferences:1.0.0 库,我已将它添加到我的类路径中。

根据https://developer.android.com/topic/libraries/architecture/datastore 描述,我已将这行代码添加到顶层的 kotline 文件中:

val Context.prefsDataStore: DataStore by preferencesDataStore(name = "settings")

但是我得到一个编译错误:

e: ...SettingsViewModel.kt: (13, 50): Property delegate must have a 'getValue(Context, KProperty<*>)' method. None of the following functions is suitable: 
public abstract operator fun getValue(thisRef: Context, property: KProperty<*>): DataStore<Preferences> defined in kotlin.properties.ReadOnlyProperty

如何使用数据存储首选项?

我的 build.gradle 文件:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'

}

apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlinx-serialization'


android {
    compileSdk 31

    defaultConfig {
        applicationId "hu.homedashboard.mobile"
        minSdk 22
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion "$kotlinVersion"
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation "androidx.activity:activity-compose:1.3.1"
    implementation "androidx.appcompat:appcompat:1.3.1"
    implementation "androidx.datastore:datastore-preferences:1.0.0"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "com.google.accompanist:accompanist-swiperefresh:0.20.3"
    implementation "androidx.core:core-ktx:1.6.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.navigation:navigation-compose:2.4.0-alpha10"
    implementation "com.google.android.material:material:1.4.0"
    implementation "com.google.dagger:hilt-android:2.40.1"
    implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
    implementation "org.ocpsoft.prettytime:prettytime:5.0.2.Final"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'

    kapt "com.google.dagger:hilt-compiler:2.38.1"
    kapt "com.google.dagger:dagger-android-processor:2.40.1"
    kapt "com.google.guava:guava:31.0.1-android"

    api "com.google.guava:guava:31.0.1-android"

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}

kapt {
    correctErrorTypes true
    javacOptions {
        option("-Xmaxerrs", 500)
    }
}

【问题讨论】:

  • getValue 通常是扩展函数,需要单独导入。尝试右键单击错误,看看是否有自动修复导入函数。
  • this one 可能会有所帮助
  • 自动修复不可用

标签: android kotlin android-jetpack-compose android-jetpack-datastore


【解决方案1】:

由于导入不正确,我收到此错误:

import java.util.prefs.Preferences

所以修复它

import androidx.datastore.preferences.core.Preferences

val Context.dataStore by preferencesDataStore(name = "settings")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2013-03-08
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多