DataStore in a Compose Multiplatform for iOS/Android — Complete Guide
For those who are not familiar, DataStore is a library that allows you to store a simple data, locally. It’s not a database. But there are two different artifacts that are available. DataStore Preferences, that allows you to store simple KEY/VALUE pairs, and a Proto DataStore that stores data as instances of a custom data type.
Creating the project
In this article I’ll show you how to utilize DataStore Preferences, because that’s what you’re gonna use in most cases. First open up a KMP wizard to generate a new Compose Multiplatform project. Select both Android/iOS with a shared UI. Then download the project.
Dependencies
After that, open up a version catalog file and add a new dependency for a DataStore preferences. Here we’re gonna need one more dependency called “kotlinx:atomicfu”. This library will allow us to create a thread-safe singleton instance of our DataStore Preferences object. I’ll show you that in a moment. Be sure to include these two in the composeApp build.gradle as well.
[versions]
...
dataStore = "1.1.1"
atomicFu =…