What types of data can SharedPreferences store?
SharedPreferences supports 5 types: String, bool, int, double, and List<String>.
Store simple key-value data persistently on the device
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
SharedPreferences stores small key-value pairs persistently on the device. It is the simplest form of local storage in Flutter and is perfect for saving user settings, preferences, and small pieces of data like the chosen theme, whether the user has completed onboarding, or the last selected language. The data survives app restarts because it is written to a file on disk. However, it is not encrypted and should never be used for sensitive information like passwords or tokens.
1// Add to pubspec.yaml: shared_preferences: ^2.2.02import 'package:shared_preferences/shared_preferences.dart';34// ── Save Data ──5Future<void> saveSettings() async {6final prefs = await SharedPreferences.getInstance();78await prefs.setString('username', 'FlutterDev');9await prefs.setBool('darkMode', true);10await prefs.setInt('loginCount', 5);11await prefs.setDouble('fontSize', 16.0);12await prefs.setStringList('favorites', ['item1', 'item2']);13}1415// ── Read Data ──16Future<void> loadSettings() async {17final prefs = await SharedPreferences.getInstance();1819final username = prefs.getString('username') ?? 'Guest';20final darkMode = prefs.getBool('darkMode') ?? false;21final loginCount = prefs.getInt('loginCount') ?? 0;22final fontSize = prefs.getDouble('fontSize') ?? 14.0;23final favorites = prefs.getStringList('favorites') ?? [];24}2526// ── Remove Data ──27Future<void> clearData() async {28final prefs = await SharedPreferences.getInstance();29await prefs.remove('username'); // remove one key30await prefs.clear(); // remove everything31}
Use SharedPreferences for small, simple data like settings. For structured data, lists of objects, or large datasets, use Hive or SQLite instead.
Treat SharedPreferences keys as a public contract in your app. Decide naming and defaults up front:
This discipline prevents silent bugs where one screen writes a key in one format and another screen reads a different format.
Store simple key value data persistently on the device. This lesson adds one more building block for creating stable and user-friendly Flutter apps.
Think in architecture, reliability, and team-level maintainability.
After reading, rewrite the concept in your own words and apply it in a tiny runnable sample.
Each lesson becomes useful when you connect concept, implementation, and real-user outcome together.
Connect this lesson to separation of concerns so your codebase stays maintainable as it grows.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
SharedPreferences supports 5 types: String, bool, int, double, and List<String>.
SharedPreferences.getInstance() is an async factory that returns the singleton instance.
Non-existent keys return null, which is why the ?? operator is used to provide defaults.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue