Introduction
Not every app needs a full database to store user data—sometimes, all you need is a way to persist small, simple values like user preferences, theme modes, or high scores. Flutter’s shared_preferences plugin offers a lightweight, key-value storage solution perfect for these use cases. It allows you to read and write primitive types locally with ease, providing a simple API that integrates seamlessly into any Flutter project.
In this guide, you’ll learn how to set up shared_preferences, save and retrieve values, and manage stored data efficiently. Whether you're building onboarding flows, saving user settings, or implementing feature toggles, this plugin is a must-have in your Flutter toolkit.
Setup
Add the dependency in your pubspec.yaml:
dependencies:
flutter:
sdk: flutter
shared_preferences
Run flutter pub get.
Import and initialize the plugin in your Dart code:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MyApp());
}You’ll call SharedPreferences.getInstance() inside async callbacks or state initialization.
Saving Data
Use the shared_preferences API to persist primitive types (String, int, bool, double, List). All setters return a Future.
Future<void> saveUserSettings() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('username', 'alice123');
await prefs.setBool('darkMode', true);
await prefs.setInt('highScore', 42);
}In this snippet:
setString('username', 'alice123') stores a user ID.
setBool('darkMode', true) toggles a theme flag.
setInt('highScore', 42) tracks a numeric value.
Retrieving Data
Fetching stored values is straightforward. Use default values or null checks if keys are missing.
Future<void> loadUserSettings() async {
final prefs = await SharedPreferences.getInstance();
final username = prefs.getString('username') ?? 'Guest';
final darkMode = prefs.getBool('darkMode') ?? false;
final highScore = prefs.getInt('highScore') ?? 0;
print('User: $username, Dark Mode: $darkMode, High Score: $highScore');
}Here, getString, getBool, and getInt read from local storage. The ?? operator ensures your app won’t crash on a missing key.
Clearing Data
To remove a single entry, call:
await prefs.remove('username');To wipe all stored preferences (use with caution):
remove targets one key; clear resets your entire persistent local storage for the shared_preferences namespace.
Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.
Conclusion
You’ve now mastered the basics of saving and retrieving simple data using Flutter shared preferences. This approach is perfect for user settings, onboarding flags, or light caching without a full database. For more advanced storage patterns (encrypted storage, structured objects), explore other packages like sqflite or hive.