tree: d15395bb6fbf277f09f3ea6753aeff3d5f87fbd4 [path history] [tgz]
  1. android/
  2. example/
  3. ios/
  4. lib/
  5. test/
  6. analysis_options.yaml
  7. CHANGELOG.md
  8. LICENSE
  9. pubspec.yaml
  10. README.md
packages/shared_preferences/README.md

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() {
  runApp(MaterialApp(
    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:

SharedPrefernces.setMockInitialValues (Map<String, dynamic> values);