shared_preferences 0.2.4+1 copy "shared_preferences: ^0.2.4+1" to clipboard
shared_preferences: ^0.2.4+1 copied to clipboard

outdatedDart 1 only

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

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;
  });
8818
likes
0
pub points
100%
popularity

Publisher

verified publisherflutter.dev

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

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, meta

More

Packages that depend on shared_preferences