Local Storage with shared_preferences

Google Flutter 9 min min read Updated: Nov 16, 2026 Beginner
Local Storage with shared_preferences
Beginner Topic 5 of 8

What Is It For?

shared_preferences saves small key-value pairs that stay on the device even after the app closes. It is perfect for settings such as dark mode, language, onboarding done, or the last selected tab.

Good forNot good for
Theme, language, flags, small numbersLarge lists or photos (use a database or files)
Last opened tabPasswords and tokens (use secure storage)

Install

bash flutter pub add shared_preferences

Save and Read

dart import 'package:shared_preferences/shared_preferences.dart'; Future<void> demo() async { final prefs = await SharedPreferences.getInstance(); // Save await prefs.setString('username', 'Asha'); await prefs.setInt('launchCount', 5); await prefs.setBool('darkMode', true); await prefs.setStringList('favorites', ['apple', 'mango']); // Read (returns null if the key does not exist) final name = prefs.getString('username') ?? 'Guest'; final dark = prefs.getBool('darkMode') ?? false; // Delete await prefs.remove('username'); await prefs.clear(); // remove everything }
API note: Newer versions of the package also offer SharedPreferencesAsync and SharedPreferencesWithCache. The ideas are identical. Check the package page for the latest recommendation.

Example: Remember Dark Mode

dart class ThemeController extends ChangeNotifier { ThemeMode _mode = ThemeMode.light; ThemeMode get mode => _mode; Future<void> load() async { final prefs = await SharedPreferences.getInstance(); final isDark = prefs.getBool('darkMode') ?? false; _mode = isDark ? ThemeMode.dark : ThemeMode.light; notifyListeners(); } Future<void> toggle() async { _mode = _mode == ThemeMode.dark ? ThemeMode.light : ThemeMode.dark; notifyListeners(); final prefs = await SharedPreferences.getInstance(); await prefs.setBool('darkMode', _mode == ThemeMode.dark); } } Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); // needed before async work in main final theme = ThemeController(); await theme.load(); runApp(MyApp(theme: theme)); }

Saving Objects

Convert an object to JSON text, then store it as a String.

dart await prefs.setString('user', jsonEncode(user.toJson())); final text = prefs.getString('user'); if (text != null) { final user = User.fromJson(jsonDecode(text) as Map<String, dynamic>); }

Secrets: flutter_secure_storage

SharedPreferences is not encrypted. For tokens and passwords use secure storage, which uses the Keychain on iOS and encrypted storage on Android.

bash flutter pub add flutter_secure_storage
dart const storage = FlutterSecureStorage(); await storage.write(key: 'token', value: 'abc123'); final token = await storage.read(key: 'token'); await storage.delete(key: 'token');
Tip: Keep key names in constants, such as const kDarkModeKey = 'darkMode';, to avoid typing mistakes.

Conclusion

shared_preferences is the quickest way to remember small things between app launches. Use secure storage for anything secret.

Quick Summary: Use getInstance, setX and getX for small settings, call WidgetsFlutterBinding.ensureInitialized in main and use secure storage for tokens.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators