flutter SharedPreferences

import 'package:shared_preferences/shared_preferences.dart';

class SPDataUtils {
  // 保存数据
  static Future setString(key, value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.setString(key, value);
  }

  // 清除数据
  static Future deleteDataInfo(key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.remove(key);
  }

  // 判断是否存在数据
  static Future hasKey(String key) async {
    Set keys = await getKeys();
    return keys.contains(key);
  }

  ///可能为空
  static get(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.get(key);
  }

  static Future getString(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var strData = prefs.getString(key);
    return strData == null ? "" : strData;
  }

  static Future putString(String key, String value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setString(key, value);
  }

  static Future getBool(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var boolData = prefs.getBool(key);
    return boolData == null ? false : boolData;
  }

  static Future putBool(String key, bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setBool(key, value);
  }

  static Future getInt(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var intData = prefs.getInt(key);
    return intData == null ? -1 : intData;
  }

  static Future putInt(String key, int value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setInt(key, value);
  }

  static Future getDouble(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var doubleData = prefs.getDouble(key);
    return doubleData == null ? 0.0 : doubleData;
  }

  static Future putDouble(String key, double value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setDouble(key, value);
  }

  static Future> getStringList(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var list = prefs.getStringList(key);
    return list == null ? [] : list;
  }

  static Future putStringList(String key, List value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setStringList(key, value);
  }

  static dynamic getDynamic(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.get(key);
  }

  static Future remove(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.remove(key);
  }

  static Future clear() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.clear();
  }

  static Future getKeys() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var data = prefs.getKeys();
    return data == null ? [] : data;
  }
}

 

你可能感兴趣的:(flutter)