shared_preferences 0.2.3

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 automatically and asynchronously.

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() {
  runApp(new MaterialApp(
    home: new Scaffold(
      body: new Center(
      child: new RaisedButton(
        onPressed: _incrementCounter,
        child: new Text('Increment Counter'),

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

[0.2.3] - 2017-06-02

  • Suppress warning about unchecked operations when compiling for Android

[0.2.2] - 2017-05-17

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

[0.2.1] - 2017-05-17

  • Support arbitrary length integers for setInt.

[0.2.0+1] - 2017-05-16

  • Updated README

[0.2.0] - 2017-05-10

[0.1.1] - 2017-05-09

  • Upgrade Android SDK Build Tools to 25.0.3.

[0.1.0] - 2017-05-05

  • Initial Open Source release.


// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

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

void main() {
  runApp(new MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'SharedPreferences Demo',
      home: new SharedPreferencesDemo(),

class SharedPreferencesDemo extends StatefulWidget {
  SharedPreferencesDemo({ Key key }) : super(key: key);

  SharedPreferencesDemoState createState() => new SharedPreferencesDemoState();

class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  Future<Null> _incrementCounter() async {
    SharedPreferences prefs = await _prefs;
    int counter = (prefs.getInt('counter') ?? 0) + 1;
    setState(() {
      prefs.setInt("counter", counter);

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text("SharedPreferences Demo"),
      body: new Center(
        child: new FutureBuilder(
          future: _prefs,
          builder: (BuildContext context, AsyncSnapshot<SharedPreferences> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return const Text('Loading...');
            int counter = snapshot.requireData.getInt('counter') ?? 0;
            return new Text(
              'Button tapped $counter time${ counter == 1 ? '' : 's' }.\n\n'
              'This should persist across restarts.',
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),

Use this package as a library

1. Depend on it

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

  shared_preferences: ^0.2.3

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or 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.

This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0