shared_preferences 0.2.5

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);
}

Testing

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

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

[0.2.5] - 2017-08-29

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

[0.2.4+1] - 2017-06-05

  • Fixed typo in changelog

[0.2.4] - 2017-06-04

  • Added setMockInitialValues
  • Added a test
  • Updated README

[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

  • Upgrade to new plugin registration. (https://groups.google.com/forum/#!topic/flutter-dev/zba1Ynf2OKM)

[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.

1. Depend on it

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

dependencies:
  shared_preferences: "^0.2.5"

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support packages 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';

Platforms

About

A Flutter plugin for reading and writing simple key-value pairs.

Author

Flutter Team

Homepage

github.com/flutter/plugins/tree/master/packages/shared_preferences

Documentation

www.dartdocs.org/documentation/shared_preferences/0.2.5/

Uploader

zarah@google.com
goderbauer@google.com
jackson@google.com

License

BSD (LICENSE)

Published

Aug 30, 2017