Created
August 1, 2021 09:12
-
-
Save Ankit-Slnk/2e7c8fb30d22e290e3a20ab750c1ed17 to your computer and use it in GitHub Desktop.
SharedPreferences with singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:shared_preferences/shared_preferences.dart'; | |
class PreferenceUtils { | |
static Future<SharedPreferences> get _instance async => | |
_prefsInstance ??= await SharedPreferences.getInstance(); | |
static SharedPreferences _prefsInstance; | |
// call this method from iniState() function of mainApp(). | |
static Future<SharedPreferences> init() async { | |
_prefsInstance = await _instance; | |
return _prefsInstance; | |
} | |
static Future<bool> setBool(String key, bool value) async { | |
var prefs = await _instance; | |
return prefs?.setBool(key, value) ?? Future.value(false); | |
} | |
static bool getBool(String key, [bool defValue]) { | |
return _prefsInstance.getBool(key) ?? defValue ?? false; | |
} | |
static Future<bool> setString(String key, String value) async { | |
var prefs = await _instance; | |
return prefs?.setString(key, value) ?? Future.value(false); | |
} | |
static String getString(String key, [String defValue]) { | |
return _prefsInstance.getString(key) ?? defValue ?? ""; | |
} | |
static Future<bool> setStringList(String key, [List<String> value]) async { | |
var prefs = await _instance; | |
return prefs?.setStringList(key, value) ?? Future.value(false); | |
} | |
static List<String> getStringList(String key, [List<String> defValue]) { | |
return _prefsInstance.getStringList(key) ?? defValue ?? false; | |
} | |
static Future<bool> remove(String key) async { | |
var prefs = await _instance; | |
return prefs?.remove(key) ?? Future.value(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment