What is shared_preferences?
- Flutter course
- Intermediate
- Lesson 36/99
- 4 min
Store simple key-value pairs with shared_preferences.
What is shared_preferences?
shared_preferences is a package used to persistently store simple key-value data in your app. It is suitable for user preferences, settings, and small flags; not for large files or sensitive data. Data survives even when the app is closed.
import 'package:shared_preferences/shared_preferences.dart';
final prefs = await SharedPreferences.getInstance();
await prefs.setString('name', 'Ada');Key-value structure
In shared_preferences, each piece of data is stored with a key (String) and the value is a primitive type: String, int, bool, double, or a String list. Writing to the same key overwrites the old value. If the key doesn't exist when reading, null is returned.
// write
await prefs.setString('theme', 'dark');
await prefs.setBool('loggedIn', true);
// read
prefs.getString('theme'); // 'dark' or nullPractice Store a value with shared_preferences
Import the shared_preferences package, get prefs with getInstance, write 'merhaba' to the 'greeting' key, and print the value.
Starter code
import 'package:flutter/material.dart';
// shared_preferences paketini içe aktar
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// getInstance ile prefs al
// 'greeting' anahtarına 'merhaba' yaz
// değeri print ile bastır
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(body: Center(child: Text('shared_preferences')));
}
}Sample solution
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
await prefs.setString('greeting', 'merhaba');
print(prefs.getString('greeting'));
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(body: Center(child: Text('shared_preferences')));
}
}Expected output
merhaba
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What kind of data is suitable to store with shared_preferences?
- Thousands of image files
- Passwords and credit card numbers
- Real-time chat messages
- User theme and settings Correct answer
Explanation: shared_preferences is designed for small, primitive key-value pairs; it is not suitable for large files or sensitive data.
2 Import the shared_preferences package
import 'package:____.dart';
import 'package:shared_preferences/shared_preferences.dart';
Explanation: The import path for the shared_preferences package is package:shared_preferences/shared_preferences.dart.
3 shared_preferences parts
| Term | Meaning |
|---|---|
shared_preferences |
Package name |
anahtar |
String identifier |
değer |
Stored data |
kalıcı |
Survives app close |