hoanglm4 / flutter_shared_preferences

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shared preferences plugin

pub package

Wraps platform-specific persistent storage for simple data (NSUserDefaults on iOS and macOS, SharedPreferences on Android, etc.). Data may be persisted to disk asynchronously, and there is no guarantee that writes will be persisted to disk after returning, so 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:

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

About

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Dart 29.6%Language:C++ 25.2%Language:CMake 17.3%Language:Java 12.4%Language:Objective-C 6.4%Language:Ruby 4.3%Language:C 2.2%Language:Swift 2.1%Language:HTML 0.4%Language:Kotlin 0.2%