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