Home > Software engineering >  Which dependency should be added in order to use "edit" extention for SharePreferences?
Which dependency should be added in order to use "edit" extention for SharePreferences?

Time:12-05

I am following the example described here - enter image description here

So, looks like the problem or I missed some dependency or I use the wrong one.

After some reserch I found out that I missed this class

package androidx.core.content

import android.annotation.SuppressLint
import android.content.SharedPreferences

/**
 * Allows editing of this preference instance with a call to [apply][SharedPreferences.Editor.apply]
 * or [commit][SharedPreferences.Editor.commit] to persist the changes.
 * Default behaviour is [apply][SharedPreferences.Editor.apply].
 * ```
 * prefs.edit {
 *     putString("key", value)
 * }
 * ```
 * To [commit][SharedPreferences.Editor.commit] changes:
 * ```
 * prefs.edit(commit = true) {
 *     putString("key", value)
 * }
 * ```
 */
@SuppressLint("ApplySharedPref")
public inline fun SharedPreferences.edit(
    commit: Boolean = false,
    action: SharedPreferences.Editor.() -> Unit
) {
    val editor = edit()
    action(editor)
    if (commit) {
        editor.commit()
    } else {
        editor.apply()
    }
}

from here - https://developer.android.com/reference/kotlin/androidx/core/content/package-summary

Then I tried to add this dependency

implementation "androidx.core.content:1.0.0"

but I still get the same error.

What am I missing?

CodePudding user response:

"androidx.core:core-ktx:$core_version"

https://developer.android.com/kotlin/ktx/extensions-list#dependency_6

  • Related