Member-only story
The Ultimate Guide to Cache and Local Storage in Flutter: Comparing Shared Preferences, GetStorage, Hive, and Secure Storage with Key Types
6 min readJan 13, 2025
Shared Preferences
A straightforward storage method for key-value pairs, ideal for saving small amounts of data, such as user preferences and settings.
Code
Future<void> sharedPrefBenchmarks() async {
final random = Random();
final Map<String, String> testData = {};
for (int i = 0; i < 1000; i++) {
testData['key$i'] = 'value${random.nextInt(10000)}';
}
benchmark(() async {
final prefs = await SharedPreferences.getInstance();
for (var entry in testData.entries) {
prefs.setString(entry.key, entry.value);
}
}, 'Shared Preferences Write Time');
benchmark(() async {
final prefs = await SharedPreferences.getInstance();
for (var key in testData.keys) {
prefs.getString(key);
}
}, 'Shared Preferences Read Time');
}