Skip to content
tezvyn:
Menu

shared_preferences for Simple On-Device Storage

Source: pub.devEasyHow cards are made

shared_preferences is Flutter's go-to for simple on-device key-value storage, like a persistent dictionary for user settings. Use it to save a dark mode toggle or high score. Crucially, writes are not guaranteed, so don't use it for critical data.

Why it exists

Apps often need to remember small pieces of data between launches, like user preferences (e.g., dark mode) or simple state (e.g., has the user seen the intro tour?). shared_preferences provides a simple, cross-platform API for this without needing a full database.

The mental model

Think of shared_preferences as a small, persistent Map<String, dynamic> stored on the device. You give it a key (like 'darkModeEnabled') and a value (like true), and you can retrieve that value later, even after the app restarts. It's for simple data, not complex or critical information.

How it works

The plugin wraps the native platform's standard for simple data storage: NSUserDefaults on iOS/macOS and SharedPreferences (or the newer DataStore) on Android. When you call a set method (e.g., setInt), the plugin passes this data to the native layer to be written to disk asynchronously. This means the write operation happens in the background and your app doesn't wait for it to complete. Newer APIs like SharedPreferencesAsync make all calls asynchronous to avoid cache inconsistencies, while older APIs use a cache for faster synchronous reads after an initial load.

When to use it

Use it for storing simple, non-critical user preferences and application state. Examples include: saving a theme choice (light/dark), remembering a user's name for a welcome message, or storing a high score in a simple game. It's perfect for data that is convenient to remember but not catastrophic if lost.

When not to use it

Do not use shared_preferences for critical data. Because writes are asynchronous and not guaranteed to persist if the app is killed, it's unsuitable for anything that must be saved reliably. Avoid it for storing sensitive information, complex relational data, or large amounts of data. For those cases, use a more robust solution like a SQLite database (via sqflite) or secure storage (flutter_secure_storage).

One canonical example

To save a user's choice to enable dark mode, you would first get an instance of the preferences, then use the setBool method with a unique key. For example: final SharedPreferences prefs = await SharedPreferences.getInstance(); followed by await prefs.setBool('darkMode', true);. To read this value back when the app starts, you would use the getBool method: final bool? isDarkMode = prefs.getBool('darkMode');.

Interview question

What is a primary reason shared_preferences is NOT recommended for storing critical application data?

  • a.Data stored with it is automatically cleared when the app is updated or reinstalled.
  • b.It only supports basic data types like strings and integers, not complex objects.
  • c.It is not cross-platform and requires separate implementations for iOS and Android.
  • d.Writes are asynchronous and not guaranteed to complete before the app might be terminated.Correct
Why?

The card explicitly states that 'writes are asynchronous and not guaranteed to persist if the app is killed,' making it unsuitable for critical data. While it primarily handles basic data types, the non-guaranteed write mechanism is the fundamental reason for its unsuitability for critical information, not merely the data types it supports.

Just read this? Test yourself on what you have been reading.

Read the original → pub.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles