Skip to content

Instantly share code, notes, and snippets.

@nehal076
Last active May 25, 2022 10:56
Show Gist options
  • Save nehal076/7424a0b674c6ee2b8aa2548f447fb400 to your computer and use it in GitHub Desktop.
Save nehal076/7424a0b674c6ee2b8aa2548f447fb400 to your computer and use it in GitHub Desktop.
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefs {
static final SharedPrefs _instance = SharedPrefs._ctor();
factory SharedPrefs() {
return _instance;
}
SharedPrefs._ctor();
static late SharedPreferences _prefs;
static init() async {
_prefs = await SharedPreferences.getInstance();
}
static setValue(String key, String value) {
_prefs.setString(key, value);
}
static String getValue(String key) {
return _prefs.getString(key) ?? "";
}
static setListValue(String key, List<String> list) {
_prefs.setStringList(key, list);
}
static List<String> getListValue(String key) {
return _prefs.getStringList(key) ?? [];
}
static Map<String, dynamic> getAllPrefs() {
final keys = _prefs.getKeys();
final Map<String, dynamic> prefsMap = {};
for (String key in keys) {
prefsMap[key] = _prefs.get(key);
}
return prefsMap;
}
static removeValue(String key) {
return _prefs.remove(key);
}
static clear() {
return _prefs.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment