import 'package:get/get.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sk_base_mobile/store/auth.store.dart'; class StorageService extends GetxService { static StorageService get to => Get.find(); late final SharedPreferences _prefs; Future init() async { _prefs = await SharedPreferences.getInstance(); return this; } Future setString(String key, String value, {bool isWithUser = true}) async { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return await _prefs.setString(storeKey, value); } Future setBool(String key, bool value, {bool isWithUser = true}) async { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return await _prefs.setBool(storeKey, value); } Future setInt(String key, int value, {bool isWithUser = true}) async { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return await _prefs.setInt(storeKey, value); } Future setList(String key, List value, {bool isWithUser = true}) async { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return await _prefs.setStringList(storeKey, value); } String? getString(String key, {bool isWithUser = true}) { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return _prefs.getString(storeKey); } int? getInt(String key, {bool isWithUser = true}) { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return _prefs.getInt(storeKey); } bool? getBool(String key, {bool isWithUser = true}) { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return _prefs.getBool(storeKey) ?? false; } List getList(String key, {bool isWithUser = true}) { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return _prefs.getStringList(storeKey) ?? []; } Future remove(String key, {bool isWithUser = true}) async { String storeKey = key; if (isWithUser) { storeKey = '${AuthStore.to.userInfo.value.userId}_$key'; } return await _prefs.remove(storeKey); } //Warning. if you use this method, all users data will be deleted Future clear() async { return await _prefs.clear(); } }