# StorageDomain.DataStorePreference

Artifact: [androidx.test.backup:backup-host](https://developer.android.com/jetpack/androidx/releases/test-backup) [View Source](https://cs.android.com/search?q=file:androidx/test/backup/host/StorageDomain.kt+class:androidx.test.backup.host.StorageDomain.DataStorePreference) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

*** ** * ** ***


```
class StorageDomain.DataStorePreference : StorageDomain
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.test.backup.host.StorageDomain](https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain) ||
|   | ↳ | [androidx.test.backup.host.StorageDomain.DataStorePreference](https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference) |

*** ** * ** ***

Targets a preference of an on-device Preferences DataStore.

The DataStore file is `datastore/<dataStoreName>.preferences_pb` in the files directory of the app, where `preferencesDataStore(dataStoreName)` keeps it. With `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#isDeviceProtectedStorage()`, it's that path in the files directory of device-protected storage, which `deviceProtectedDataStoreFile("<dataStoreName>.preferences_pb")` returns. `preferencesDataStoreFile` always returns the first path, even for a device-protected context.

The Kotlin type of `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#(kotlin.String,kotlin.String,kotlin.Any,kotlin.Boolean)` sets the type of the preference key, such as `1.5f` for `floatPreferencesKey` and `1.5` for `doublePreferencesKey`. Use the type of your app's key: with another type, the verification passes, but your app can't read the value.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#DataStorePreference(kotlin.String,kotlin.String,kotlin.Any,kotlin.Boolean)( dataStoreName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, isDeviceProtectedStorage: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` |

| ### Public functions |
|---|---|
| `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#equals(kotlin.Any)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#hashCode()()` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#toString()()` |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#dataStoreName()` The name of the Preferences DataStore, as the app passes it to `preferencesDataStore`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#isDeviceProtectedStorage()` Whether the Preferences DataStore file is in device-protected storage, which is available before the user first unlocks the device, rather than in the default storage of the app. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#key()` The preference key to seed or verify. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#value()` The value associated with `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#key()`, or `null` if asserting missing state. |

## Public constructors

### DataStorePreference

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
DataStorePreference(
    dataStoreName: String,
    key: String,
    value: Any? = null,
    isDeviceProtectedStorage: Boolean = false
)
```

## Public functions

### equals

```
open operator fun equals(other: Any?): Boolean
```

### hashCode

```
open fun hashCode(): Int
```

### toString

```
open fun toString(): String
```

## Public properties

### dataStoreName

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
val dataStoreName: String
```

The name of the Preferences DataStore, as the app passes it to `preferencesDataStore`.

### isDeviceProtectedStorage

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
val isDeviceProtectedStorage: Boolean
```

Whether the Preferences DataStore file is in device-protected storage, which is available before the user first unlocks the device, rather than in the default storage of the app. That is credential-encrypted storage, unless the app sets `android:defaultToDeviceProtectedStorage`.

### key

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
val key: String
```

The preference key to seed or verify.

### value

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
val value: Any?
```

The value associated with `https://developer.android.com/reference/kotlin/androidx/test/backup/host/StorageDomain.DataStorePreference#key()`, or `null` if asserting missing state. Supported types are: String, Int, Long, Float, Double, Boolean, ByteArray, a Set of Strings, or null; other types fail construction with an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. The domain keeps a copy of a set or byte array, and returns a new copy of a byte array on each read. A `null` value is seeded by removing the key and verified by checking that the key is absent. Clearing app data also removes the key, so a `null` value cannot detect a restore that loses data.