-
-
Save faisalraja/9b628439978c67b2fb6515cf202a3992 to your computer and use it in GitHub Desktop.
class Preference { | |
static SharedPreferences _prefs; | |
static Map<String, dynamic> _memoryPrefs = Map<String, dynamic>(); | |
static Future<SharedPreferences> load() async { | |
if (_prefs == null) { | |
_prefs = await SharedPreferences.getInstance(); | |
} | |
return _prefs; | |
} | |
static void setString(String key, String value) { | |
_prefs.setString(key, value); | |
_memoryPrefs[key] = value; | |
} | |
static void setInt(String key, int value) { | |
_prefs.setInt(key, value); | |
_memoryPrefs[key] = value; | |
} | |
static void setDouble(String key, double value) { | |
_prefs.setDouble(key, value); | |
_memoryPrefs[key] = value; | |
} | |
static void setBool(String key, bool value) { | |
_prefs.setBool(key, value); | |
_memoryPrefs[key] = value; | |
} | |
static String getString(String key, {String def}) { | |
String val; | |
if (_memoryPrefs.containsKey(key)) { | |
val = _memoryPrefs[key]; | |
} | |
if (val == null) { | |
val = _prefs.getString(key); | |
} | |
if (val == null) { | |
val = def; | |
} | |
_memoryPrefs[key] = val; | |
return val; | |
} | |
static int getInt(String key, {int def}) { | |
int val; | |
if (_memoryPrefs.containsKey(key)) { | |
val = _memoryPrefs[key]; | |
} | |
if (val == null) { | |
val = _prefs.getInt(key); | |
} | |
if (val == null) { | |
val = def; | |
} | |
_memoryPrefs[key] = val; | |
return val; | |
} | |
static double getDouble(String key, {double def}) { | |
double val; | |
if (_memoryPrefs.containsKey(key)) { | |
val = _memoryPrefs[key]; | |
} | |
if (val == null) { | |
val = _prefs.getDouble(key); | |
} | |
if (val == null) { | |
val = def; | |
} | |
_memoryPrefs[key] = val; | |
return val; | |
} | |
static bool getBool(String key, {bool def = false}) { | |
bool val; | |
if (_memoryPrefs.containsKey(key)) { | |
val = _memoryPrefs[key]; | |
} | |
if (val == null) { | |
val = _prefs.getBool(key); | |
} | |
if (val == null) { | |
val = def; | |
} | |
_memoryPrefs[key] = val; | |
return val; | |
} | |
} |
It's a wrapper for shared_preferences package. So you need to add that to your pubspec.
Then you need to make main async and call static function load:
Preference pref = await Preference.load();
Hi Faisal,
New to Flutter. May I know why are we storing key-value pairs in Map, when we are already retrieving them via SharedPreferences?
Mostly for performance. When I update a preference I usually update a UI somewhere that pulls the same config. Maybe shared preferences already do that but I really didn't look into how it works under the hood.
I see. Just in case if it hasn't crossed your mind, you can use the same config before putting the parameter into preferences.
Thanks mate!
What is the difference between _prefs and _memoryPrefs? ?? @faisalraja
I just cache the value as I set it so anytime I use the value I just access it from memory.
class Preference {
static late final SharedPreferences _prefs;
static final Map<String, dynamic> _memoryPrefs = {};
static Future<SharedPreferences> load() async {
_prefs = await SharedPreferences.getInstance();
return _prefs;
}
static void setString(String key, String value) {
_prefs.setString(key, value);
_memoryPrefs[key] = value;
}
static void setInt(String key, int value) {
_prefs.setInt(key, value);
_memoryPrefs[key] = value;
}
static void setDouble(String key, double value) {
_prefs.setDouble(key, value);
_memoryPrefs[key] = value;
}
static void setBool(String key, bool value) {
_prefs.setBool(key, value);
_memoryPrefs[key] = value;
}
static String? getString(String key) => _memoryPrefs[key] ?? _prefs.getString(key);
static int? getInt(String key) => _memoryPrefs[key] ?? _prefs.getInt(key);
static double? getDouble(String key) => _memoryPrefs[key] ?? _prefs.getDouble(key);
static bool? getBool(String key) => _memoryPrefs[key] ?? _prefs.getBool(key);
}
Your code was very helpful. I've made some edits in my own way, so I'll share.
static Future<SharedPreferences> load() async {
_prefs = await SharedPreferences.getInstance();
final keys = _prefs.getKeys();
for (String key in keys) {
_memoryPrefs[key] = _prefs.get(key);
}
return _prefs;
}
@keehyun2 It's much better now, I added this to load function
class Preferences {
// ...
static void setStringList(String key, List<String> value) {
_prefs.setStringList(key, value);
_memoryPrefs[key] = value;
}
static void remove(String key) {
_prefs.remove(key);
_memoryPrefs.remove(key);
}
static List<String>? getStringList(String key) => _memoryPrefs[key] ?? _prefs.getStringList(key);
// ...
}
I added the above methods
hi, sorry im new in flutter. How i use that? Im putting in main.dart with a declaration but dont work.
Preference prefs = Preference();