import 'dart:convert'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; class SecureStorageUtil { // 单例对象 static final SecureStorageUtil _singleton = SecureStorageUtil._internal(); // 私有构造函数 SecureStorageUtil._internal(); // 工厂构造函数返回单例对象 factory SecureStorageUtil() => _singleton; // Secure Storage 实例 final _storage = const FlutterSecureStorage(); // 存储字符串 Future setString(String key, String value) async { await _storage.write(key: key, value: value); } // 读取字符串 Future getString(String key) async { return await _storage.read(key: key); } // 存储JSON数组 Future setJsonList(String key, List> jsonList) async { final jsonString = jsonEncode(jsonList); await _storage.write(key: key, value: jsonString); } // 读取JSON数组 Future>?> getJsonList(String key) async { final jsonString = await _storage.read(key: key); return jsonString != null ? List>.from(jsonDecode(jsonString)) : null; } // 删除指定键的数据 Future delete(String key) async { await _storage.delete(key: key); } // 删除所有数据 Future deleteAll() async { await _storage.deleteAll(); } }