Persisting Data in Your Flutter App with SharedPreferences
What is SharedPreferences in Flutter?
SharedPreferences is a mechanism used by Android and iOS apps to store simple data in an allocated space. This data persists even when the app is shut down and starts up again, allowing you to retrieve the value as it was. The data stored in SharedPreferences can be edited and deleted, and is stored in a key-value pair.
Scaffolding a Flutter Project
To get started, you’ll need to have the Flutter SDK and a code editor installed on your machine. Once you’ve set up your development environment, you can scaffold a new Flutter project using the following command:
flutter create shared_pref
This command creates a new folder called shared_pref
and generates a basic Flutter project inside it.
Installing the shared_preferences Plugin
To use SharedPreferences in your Flutter app, you’ll need to install the shared_preferences
plugin. Open the pubspec.yaml
file and add the following dependency:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.0.3
Then, import the plugin into your Dart file:
import 'package:shared_preferences/shared_preferences.dart';
Using SharedPreferences in Your Flutter App
The shared_preferences
plugin provides several methods for working with SharedPreferences, including:
getInstance
getInt
getString
getBool
getDouble
remove
containsKey
Here’s an example of how to add data to SharedPreferences:
Future<void> _addData() async {
final prefs = await SharedPreferences.getInstance();
prefs.setInt('counter', 1);
prefs.setString('message', 'Hello, World!');
prefs.setBool('isAdmin', true);
prefs.setDouble('latitude', 37.7749);
}
And here’s an example of how to retrieve data from SharedPreferences:
Future<void> _retrieveData() async {
final prefs = await SharedPreferences.getInstance();
final counter = prefs.getInt('counter');
final message = prefs.getString('message');
final isAdmin = prefs.getBool('isAdmin');
final latitude = prefs.getDouble('latitude');
}
Implementing a Splash Screen with SharedPreferences
You can also use SharedPreferences to implement a splash screen in your Flutter app. Here’s an example of how to do it:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _newLaunch = true;
@override
void initState() {
super.initState();
_loadNewLaunch();
}
Future _loadNewLaunch() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_newLaunch = prefs.getBool('newLaunch')?? true;
});
}
@override
Widget build(BuildContext context) {
return _newLaunch
? SplashScreen()
: ProfileScreen();
}
}
In this example, we’re using SharedPreferences to store a boolean value called newLaunch
. If the value is true
, we show the splash screen. If the value is false
, we show the profile screen.
Testing Your Flutter App
To test your Flutter app, run the following command:
flutter run
This will launch your app on an emulator or physical device. You can then interact with your app to see how the SharedPreferences data is persisted.
By using SharedPreferences in your Flutter app, you can provide a better user experience by storing and retrieving data locally. Whether you’re building a simple todo list app or a complex enterprise app, SharedPreferences is a powerful tool that can help you achieve your goals.