SharedPreferences
SharedPreferences provides a lightweight way to store simple key-value pairs locally in Flutter apps. It is ideal for saving user preferences, flags, or other small pieces of data that need to persist between app launches but do not require the complexity of a full database.
Unlike more complex storage solutions, SharedPreferences is synchronous and easy to use, making it a go-to option for persisting primitive data types.
main.dart
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273// main.dart import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: const PreferenceDemo(), ); } } class PreferenceDemo extends StatefulWidget { const PreferenceDemo({super.key}); @override State<PreferenceDemo> createState() => _PreferenceDemoState(); } class _PreferenceDemoState extends State<PreferenceDemo> { final TextEditingController _controller = TextEditingController(); String _savedValue = ""; Future<void> _saveValue() async { final prefs = await SharedPreferences.getInstance(); await prefs.setString('myKey', _controller.text); setState(() { _savedValue = _controller.text; }); } Future<void> _loadValue() async { final prefs = await SharedPreferences.getInstance(); setState(() { _savedValue = prefs.getString('myKey') ?? ""; }); } @override void initState() { super.initState(); _loadValue(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('SharedPreferences Demo')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ TextField( controller: _controller, decoration: const InputDecoration(labelText: 'Enter a value'), ), ElevatedButton( onPressed: _saveValue, child: const Text('Save'), ), const SizedBox(height: 20), Text('Saved value: $_savedValue'), ], ), ), ); } }
SharedPreferences supports storing primitive types such as int, double, bool, String, and List<String>. Data saved with SharedPreferences persists even after the app is closed or restarted, as shown in the example above. However, it is not suitable for large or complex data structures, and you should use it only for simple, lightweight storage needs.
main.dart
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: const RemovePreferenceDemo(), ); } } class RemovePreferenceDemo extends StatefulWidget { const RemovePreferenceDemo({super.key}); @override State<RemovePreferenceDemo> createState() => _RemovePreferenceDemoState(); } class _RemovePreferenceDemoState extends State<RemovePreferenceDemo> { String _savedValue = ""; Future<void> _loadValue() async { final prefs = await SharedPreferences.getInstance(); setState(() { _savedValue = prefs.getString('myKey') ?? ""; }); } Future<void> _removeValue() async { final prefs = await SharedPreferences.getInstance(); await prefs.remove('myKey'); setState(() { _savedValue = ""; }); } @override void initState() { super.initState(); _loadValue(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Remove SharedPreferences Value')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ Text('Saved value: $_savedValue'), ElevatedButton( onPressed: _removeValue, child: const Text('Remove Value'), ), ], ), ), ); } }
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me an example of how to save and retrieve a value using SharedPreferences?
What are some common use cases for SharedPreferences in Flutter apps?
Are there any security concerns I should be aware of when using SharedPreferences?
Awesome!
Completion rate improved to 9.09
SharedPreferences
Swipe to show menu
SharedPreferences provides a lightweight way to store simple key-value pairs locally in Flutter apps. It is ideal for saving user preferences, flags, or other small pieces of data that need to persist between app launches but do not require the complexity of a full database.
Unlike more complex storage solutions, SharedPreferences is synchronous and easy to use, making it a go-to option for persisting primitive data types.
main.dart
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273// main.dart import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: const PreferenceDemo(), ); } } class PreferenceDemo extends StatefulWidget { const PreferenceDemo({super.key}); @override State<PreferenceDemo> createState() => _PreferenceDemoState(); } class _PreferenceDemoState extends State<PreferenceDemo> { final TextEditingController _controller = TextEditingController(); String _savedValue = ""; Future<void> _saveValue() async { final prefs = await SharedPreferences.getInstance(); await prefs.setString('myKey', _controller.text); setState(() { _savedValue = _controller.text; }); } Future<void> _loadValue() async { final prefs = await SharedPreferences.getInstance(); setState(() { _savedValue = prefs.getString('myKey') ?? ""; }); } @override void initState() { super.initState(); _loadValue(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('SharedPreferences Demo')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ TextField( controller: _controller, decoration: const InputDecoration(labelText: 'Enter a value'), ), ElevatedButton( onPressed: _saveValue, child: const Text('Save'), ), const SizedBox(height: 20), Text('Saved value: $_savedValue'), ], ), ), ); } }
SharedPreferences supports storing primitive types such as int, double, bool, String, and List<String>. Data saved with SharedPreferences persists even after the app is closed or restarted, as shown in the example above. However, it is not suitable for large or complex data structures, and you should use it only for simple, lightweight storage needs.
main.dart
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: const RemovePreferenceDemo(), ); } } class RemovePreferenceDemo extends StatefulWidget { const RemovePreferenceDemo({super.key}); @override State<RemovePreferenceDemo> createState() => _RemovePreferenceDemoState(); } class _RemovePreferenceDemoState extends State<RemovePreferenceDemo> { String _savedValue = ""; Future<void> _loadValue() async { final prefs = await SharedPreferences.getInstance(); setState(() { _savedValue = prefs.getString('myKey') ?? ""; }); } Future<void> _removeValue() async { final prefs = await SharedPreferences.getInstance(); await prefs.remove('myKey'); setState(() { _savedValue = ""; }); } @override void initState() { super.initState(); _loadValue(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Remove SharedPreferences Value')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ Text('Saved value: $_savedValue'), ElevatedButton( onPressed: _removeValue, child: const Text('Remove Value'), ), ], ), ), ); } }
Thanks for your feedback!