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<bool> 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<String> getString(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var strData = prefs.getString(key);
    return strData == null ? "" : strData;
  }

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

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

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

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

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

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

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

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

  static Future<bool> putStringList(String key, List<String> 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<bool> remove(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.remove(key);
  }

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

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章