FlutterIntermediate 2 min read

Flutter shared_preferences for small settings

Store a small non-sensitive preference, use the async preferences API and keep persistence separate from interface state.

On this page

Preferences are appropriate for small settings such as a theme choice or onboarding flag. They are not a relational database, a secure credential store or a guarantee that a critical transaction is durably committed.

Use an explicit asynchronous boundary

Add shared_preferences and import package:shared_preferences/shared_preferences.dart. This fragment uses the asynchronous API for a simple boolean setting.

Worked example

This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.

final preferences = SharedPreferencesAsync();

Future<void> saveReminderPreference(bool enabled) async {
  await preferences.setBool('practice_reminders', enabled);
}

Future<bool> loadReminderPreference() async {
  return await preferences.getBool('practice_reminders') ?? false;
}

Read the result

The default false is a product decision for an absent preference. It does not schedule a notification; it only stores a setting. If a save fails, decide whether the interface should revert, retry or explain that the change is temporary.

Follow a preference across two launches

The screen first loads a stored value, using the chosen default only when no value exists. A user action changes the current setting and requests a write. On a later launch, the feature reads the persisted value again rather than assuming the previous widget’s State still exists.

Decide what happens if the write fails: revert the interface, show that the setting is temporary or offer a retry. Hiding failure can make a setting appear saved until the next launch surprises the user.

Preferences fit small non-sensitive configuration. For a structured task collection with queries, use a database/repository design . For remote shared data, cloud authorization is an additional responsibility, not something local preferences can provide.

Practice and verification

Load the preference on a fresh launch, change it and relaunch. Test the missing-key case. Do not store passwords or access tokens in ordinary preferences.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.