shared_preferences 0.5.2

Shared preferences plugin #

pub package

Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing a persistent store for simple data. Data is persisted to disk asynchronously. Neither platform can guarantee that writes will be persisted to disk after returning and this plugin must not be used for storing critical data.

Usage #

To use this plugin, add shared_preferences as a dependency in your pubspec.yaml file.

Example #

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
    home: Scaffold(
      body: Center(
      child: RaisedButton(
        onPressed: _incrementCounter,
        child: Text('Increment Counter'),

_incrementCounter() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int counter = (prefs.getInt('counter') ?? 0) + 1;
  print('Pressed $counter times.');
  await prefs.setInt('counter', counter);

Testing #

You can populate SharedPreferences with initial values in your tests by running this code:

const MethodChannel('')
  .setMockMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == 'getAll') {
      return <String, dynamic>{}; // set initial values here if desired
    return null;

0.5.2 #

  • Add containsKey method.

0.5.1+2 #

  • Add a driver test

0.5.1+1 #

  • Log a more detailed warning at build time about the previous AndroidX migration.

0.5.1 #

  • Use String to save double in Android.

0.5.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

0.4.3 #

  • Prevent strings that match special prefixes from being saved. This is a bugfix that prevents apps from accidentally setting special values that would be interpreted incorrectly.

0.4.2 #

  • Updated Gradle tooling to match Android Studio 3.1.2.

0.4.1 #

  • Added getKeys method.

0.4.0 #

  • Breaking change. Set SDK constraints to match the Flutter beta release.

0.3.3 #

  • Fixed Dart 2 issues.

0.3.2 #

  • Added an getter that can retrieve values of any type

0.3.1 #

  • Simplified and upgraded Android project template to Android SDK 27.
  • Updated package description.

0.3.0 #

  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.

0.2.6 #

  • Added FLT prefix to iOS types

0.2.5+1 #

  • Aligned author name with rest of repo.

0.2.5 #

  • Fixed crashes when setting null values. They now cause the key to be removed.
  • Added remove() method

0.2.4+1 #

  • Fixed typo in changelog

0.2.4 #

  • Added setMockInitialValues
  • Added a test
  • Updated README

0.2.3 #

  • Suppress warning about unchecked operations when compiling for Android

0.2.2 #

  • BREAKING CHANGE: setStringSet API changed to setStringList and plugin now supports ordered storage.

0.2.1 #

  • Support arbitrary length integers for setInt.

0.2.0+1 #

  • Updated README

0.2.0 #

0.1.1 #

  • Upgrade Android SDK Build Tools to 25.0.3.

0.1.0 #

  • Initial Open Source release.


shared_preferences_example #

Demonstrates how to use the shared_preferences plugin.

Getting Started #

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  shared_preferences: ^0.5.2

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:shared_preferences/shared_preferences.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 28, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
meta ^1.0.4 1.1.8
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
test any