flutter数据存储封装shared

flutter数据存储封装shared,第1张

flutter数据存储封装shared
import 'package:shared_preferences/shared_preferences.dart';

// 设置
setStorage(String key, value) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  if (value is String) {
    prefs.setString(key, value);
  } else if (value is num) {
    prefs.setInt(key, value as int);
  } else if (value is double) {
    prefs.setDouble(key, value);
  } else if (value is bool) {
    prefs.setBool(key, value);
  } else if (value is List) {
    prefs.setStringList(key, value.cast());
  }
}

// 获取
getStorage(String key, [dynamic replace]) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var data = prefs.get(key);
  return data ?? replace;
}

// 移除
removeStorage(String key) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.remove(key);
}

removeAllStorage() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.clear();
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5722137.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存