1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import 'package:shared_preferences/shared_preferences.dart';
- /// 持久化缓存类
- ///
- /// 使用SharedPreferences共享参数存储
- class SPUtils {
- SPUtils._();
- static SPUtils? _instance;
- static SPUtils getInstance() => _instance ??= SPUtils._();
- late SharedPreferences _sp;
- Future<void> init() async {
- _sp = await SharedPreferences.getInstance();
- }
- /// 获取所有key
- Set<String> getKeys() {
- return _sp.getKeys();
- }
- /// 是否有key
- bool containsKey(String key) {
- return _sp.containsKey(key);
- }
- /// 以k,v形式 存储各种类型的值
- ///
- Future<bool> saveString(String key, String value) async {
- return await _sp.setString(key, value);
- }
- Future<bool> saveInt(String key, int value) async {
- return await _sp.setInt(key, value);
- }
- Future<bool> saveDouble(String key, double value) async {
- return await _sp.setDouble(key, value);
- }
- Future<bool> saveBool(String key, bool value) async {
- return await _sp.setBool(key, value);
- }
- Future<bool> saveStringList(String key, List<String> value) async {
- return await _sp.setStringList(key, value);
- }
- /// 获取存储的数据
- ///
- String? getString(String key) {
- return _sp.getString(key);
- }
- int? getInt(String key) {
- return _sp.getInt(key);
- }
- double? getDouble(String key) {
- return _sp.getDouble(key);
- }
- bool? getBool(String key) {
- return _sp.getBool(key);
- }
- List<String>? getStringList(String key) {
- return _sp.getStringList(key);
- }
- /// 移除存储的数据
- ///
- Future<bool> remove(String key) async {
- return await _sp.remove(key);
- }
- Future<bool> clear() async {
- return await _sp.clear();
- }
- }
|