Skip to content

Instantly share code, notes, and snippets.

@faisalraja
Created January 17, 2019 03:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save faisalraja/9b628439978c67b2fb6515cf202a3992 to your computer and use it in GitHub Desktop.
Save faisalraja/9b628439978c67b2fb6515cf202a3992 to your computer and use it in GitHub Desktop.
SharedPreference helper for flutter
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;
}
}
@lsboissard-dev
Copy link

hi, sorry im new in flutter. How i use that? Im putting in main.dart with a declaration but dont work.

Preference prefs = Preference();

@faisalraja
Copy link
Author

faisalraja commented Jul 3, 2019

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();

@s-sati
Copy link

s-sati commented Jul 3, 2019

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?

@faisalraja
Copy link
Author

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.

@s-sati
Copy link

s-sati commented Jul 3, 2019

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!

@real1900
Copy link

What is the difference between _prefs and _memoryPrefs? ?? @faisalraja

@faisalraja
Copy link
Author

I just cache the value as I set it so anytime I use the value I just access it from memory.

@keehyun2
Copy link

keehyun2 commented Jul 26, 2022

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.

@00Adnan00
Copy link

00Adnan00 commented Feb 27, 2023

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

@sixtusagbo
Copy link

sixtusagbo commented Mar 28, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment