Skip to content
Branch: master
Find file History
hterkelsen [shared_preferences] Endorse web plugin. (#2390)
* Make shared_preferences example work on web

* [shared_preferences] Endorse web plugin

* format pubspec
Latest commit fc2ea9f Dec 10, 2019

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.


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


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


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

SharedPrefernces.setMockInitialValues (Map<String, dynamic> values);
You can’t perform that action at this time.