Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn SharedPreferences | Local Storage
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Flutter State and Data Handling

bookSharedPreferences

Note
Definition

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

main.dart

copy
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

main.dart

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
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 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'), ), ], ), ), ); } }
question mark

Which of the following statements best describes the limitations and typical use cases of SharedPreferences in Flutter?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

bookSharedPreferences

Swipe to show menu

Note
Definition

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

main.dart

copy
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

main.dart

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
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 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'), ), ], ), ), ); } }
question mark

Which of the following statements best describes the limitations and typical use cases of SharedPreferences in Flutter?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
some-alt