> ## Documentation Index
> Fetch the complete documentation index at: https://filekit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# DataStore

> Create a Preferences DataStore from common code with FileKit.databasesDir

[See the platform support matrix](https://filekit.mintlify.site/platform-support.md#filekit-core)

The [official DataStore setup](https://developer.android.com/kotlin/multiplatform/datastore) needs an `expect`/`actual` function to find a file path on each platform. `FileKit.databasesDir` gives you that path from shared code, so the whole setup fits in one file.

## Dependencies

```kotlin build.gradle.kts theme={null}
commonMain.dependencies {
    implementation("io.github.vinceglb:filekit-core:<version>")
    implementation("androidx.datastore:datastore-preferences:<version>")
}
```

`datastore-preferences` 1.3.0 publishes no Windows native or web targets, and `databasesDir` doesn't exist on the web. If your app has those targets, add the dependency and the code below to a source set shared only by the platforms above instead of `commonMain`.

## Create the DataStore

```kotlin theme={null}
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import io.github.vinceglb.filekit.FileKit
import io.github.vinceglb.filekit.databasesDir
import io.github.vinceglb.filekit.path
import io.github.vinceglb.filekit.resolve
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import okio.Path.Companion.toPath

object AppPreferences {
    private val dataStore: DataStore<Preferences> = PreferenceDataStoreFactory.createWithPath(
        produceFile = { FileKit.databasesDir.resolve("app.preferences_pb").path.toPath() },
    )

    private val darkModeKey = booleanPreferencesKey("dark_mode")

    val darkMode: Flow<Boolean> = dataStore.data.map { it[darkModeKey] ?: false }

    suspend fun setDarkMode(enabled: Boolean) {
        dataStore.edit { it[darkModeKey] = enabled }
    }
}
```

DataStore expects an okio `Path`, so the last step converts FileKit's path string with okio's `toPath()`. DataStore creates the parent folder when it first writes, so `databasesDir` doesn't need to exist yet on desktop.

Create only one DataStore per file. A singleton `object`, or a single instance from your dependency injection, keeps that guarantee.

On JVM and Linux native, call `FileKit.init(appId)` before the first read. See [Core setup](https://filekit.mintlify.site/core/setup.md).

## Where the file goes

| Platform | `databasesDir` |
| - | - |
| Android | The app's `databases/` directory |
| iOS, watchOS | `Documents/databases/` |
| macOS | `~/Library/Application Support/<bundle id>/databases/` |
| JVM, Linux native | `<filesDir>/databases/`, for example `~/.local/share/<appId>/databases/` on Linux |

See [App directories](https://filekit.mintlify.site/core/file-utils.md#directory-locations) for every platform.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.