Intermediate
Local Data Storage in Flutter: Hive vs SharedPreferences vs Secure Storage
· 9 min read
Three Different Needs, Three Different Tools
"Local data storage" isn't a single problem. A user's theme preference, a cached list of articles, and a session token don't share the same sensitivity or the same shape. Trying to solve all of them with the same tool (usually SharedPreferences) leads either to performance problems or, worse, security holes. The Flutter ecosystem has three separate, well-established solutions for these three needs.
SharedPreferences: Simple Key-Value Settings
The shared_preferences package is a thin layer over NSUserDefaults on iOS and SharedPreferences on Android. It only stores primitive types like int, double, bool, String and List<String>, works fully asynchronously, and does no encryption. It's ideal for small, non-sensitive settings such as theme mode, whether onboarding has been shown, or the last selected language.
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('onboarding_seen', true);
await prefs.setString('theme_mode', 'dark');
final themeMode = prefs.getString('theme_mode') ?? 'system';Hive: A Fast, Structured Local Database
Hive is a pure-Dart NoSQL database with no native dependencies. Unlike SharedPreferences, it can store real objects (via TypeAdapter) and keeps them in collections called boxes. For structured and potentially large data — an offline article list, cart contents, or form drafts that need to sync later — it's a much better fit than SharedPreferences.
@HiveType(typeId: 0)
class CachedArticle extends HiveObject {
CachedArticle({required this.id, required this.title});
@HiveField(0)
final String id;
@HiveField(1)
final String title;
}
// Run once, during app startup
await Hive.initFlutter();
Hive.registerAdapter(CachedArticleAdapter());
final box = await Hive.openBox<CachedArticle>('articles');
await box.put(article.id, article);
final cached = box.get('flutter-wrap');flutter_secure_storage: For Sensitive Data
flutter_secure_storage stores data in the Keychain on iOS and in Keystore-backed EncryptedSharedPreferences on Android. Reads and writes are slower than plain SharedPreferences and it isn't designed for large datasets — but for small, critical secrets like an access token, a refresh token, or a biometric lock key, it's the right tool.
const storage = FlutterSecureStorage();
await storage.write(key: 'auth_token', value: token);
final token = await storage.read(key: 'auth_token');
await storage.delete(key: 'auth_token');Which Data Goes Where?
• Small, non-sensitive settings such as theme, language, notification permission → SharedPreferences. • Cached lists, drafts, object collections, large structured data → Hive. • Session tokens, refresh tokens, password-like secrets → flutter_secure_storage. • If you need complex relational queries (joins, filtering) → a full SQL database such as sqflite or drift is probably a better fit.
Things to Watch Out For
• NEVER store a token, password or any credential as plain text in SharedPreferences; this data can be read easily if the device is rooted or jailbroken. • Always open Hive boxes with a specific name, and use separate boxes for different data types; writing mixed-type objects into the same box is error-prone. • For data in Hive that's somewhat sensitive but not as critical as a token (e.g. a cached user profile), you can add encryption with an encryptionCipher, but real secrets should still live in secure storage. • All three solutions initialize asynchronously; don't try to read the relevant data before awaiting the required box/read operations to finish during app startup.
Conclusion
Instead of squeezing all local data into one tool, classify it first: is it a simple setting, a structured cache, or a sensitive secret? The answer points you straight to SharedPreferences, Hive or flutter_secure_storage. Especially for tokens and credentials, this choice isn't a performance preference — it's a security requirement.