Saving and reading data in Flutter with SharedPreferences

2 min readJan 4, 2019

In Flutter, Shared Preferences are used to store primitive data (int, double, bool, string, and stringList). This data is associated with the app, so when the user uninstalls your app, the data will also be deleted. It is meant for storing small amounts of data.

The following steps will tell you how to use SharedPreferences.

Get the plugin

The shared_preferences plugin from pub is a wrapper around Android SharedPreferences and iOS NSUserDefaults. You can get this plugin by adding the shared_preferences line to your pubspec.yaml file in the dependencies section.

sdk: flutter
shared_preferences: '>=0.5.12+4 <2.0.0'

Change the version number to whatever the current one is.

Import the package

In whichever file you need the Shared Preferences, add the following import:

import 'package:shared_preferences/shared_preferences.dart';

Reading and writing data

To get the shared preferences object you can do the following:

final prefs = await SharedPreferences.getInstance();

This will be used for all of the following examples.


// read
final myInt = prefs.getInt('my_int_key') ?? 0;
// write
prefs.setInt('my_int_key', 42);


// read
final myDouble = prefs.getDouble('my_double_key') ?? 0.0;
// write
prefs.setBool('my_bool_key', true);


// read
final myBool = prefs.getBool('my_bool_key') ?? false;
// write
prefs.setBool('my_bool_key', true);


// read
final myString = prefs.getString('my_string_key') ?? '';
// write
prefs.setString('my_string_key', 'hello');


// read
final myStringList = prefs.getStringList('my_string_list_key') ?? [];
// write…

Flutter and Dart developer. Twitter: @suragch1