Skip to content

Instantly share code, notes, and snippets.

@berkanaslan
Created May 25, 2021 08:51
Show Gist options
  • Save berkanaslan/8790b136dfc684f65a102ee39d88a7df to your computer and use it in GitHub Desktop.
Save berkanaslan/8790b136dfc684f65a102ee39d88a7df to your computer and use it in GitHub Desktop.
Flutter Shared Preferences Helper
import 'package:online_tpi_qms_mobile/model/user/user.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefs {
static SharedPreferences _sharedPrefs;
static Map<String, dynamic> _memoryPrefs = Map<String, dynamic>();
factory SharedPrefs() => SharedPrefs._internal();
SharedPrefs._internal();
Future<void> init() async {
_sharedPrefs ??= await SharedPreferences.getInstance();
// We deleted the before user data because of we don't hold sessions.
removeUserData();
}
// ---------------------------
// User
// ---------------------------
final String userKey = "user";
Future<void> setUser(User user) async {
_memoryPrefs[userKey] = jsonEncode(user);
await _sharedPrefs.setString(userKey, jsonEncode(user));
}
User getUser() {
String userMap;
User user;
if (_memoryPrefs.containsKey(userKey)) {
userMap = _sharedPrefs.getString(userKey);
if (userMap != null) user = User.fromJson(json.decode(userMap));
}
if (user == null) {
userMap = _sharedPrefs.getString(userKey);
if (userMap != null) user = User.fromJson(json.decode(userMap));
}
return user;
}
// ---------------------------
// General Shared Preferences Helper
// ---------------------------
// String
static void setString(String key, String value) {
_memoryPrefs[key] = value;
_sharedPrefs.setString(key, value);
}
static String getString(String key, {String defaultValue}) {
String value;
if (_memoryPrefs.containsKey(key)) value = _memoryPrefs[key];
if (value == null) value = _sharedPrefs.getString(key);
if (value == null) value = defaultValue;
return value;
}
// Int
static void setInt(String key, int value) {
_memoryPrefs[key] = value;
_sharedPrefs.setInt(key, value);
}
static int getInt(String key, {int defaultValue}) {
int value;
if (_memoryPrefs.containsKey(key)) value = _memoryPrefs[key];
if (value == null) value = _sharedPrefs.getInt(key);
if (value == null) value = defaultValue;
return value;
}
// Bool
static void setBool(String key, bool value) {
_memoryPrefs[key] = value;
_sharedPrefs.setBool(key, value);
}
static bool getBool(String key, {bool defaultValue = false}) {
bool value;
if (_memoryPrefs.containsKey(key)) value = _memoryPrefs[key];
if (value == null) value = _sharedPrefs.getBool(key);
if (value == null) value = defaultValue;
return value;
}
// Double
static void setDouble(String key, double value) {
_memoryPrefs[key] = value;
_sharedPrefs.setDouble(key, value);
}
static double getDouble(String key, {double defaultValue}) {
double value;
if (_memoryPrefs.containsKey(key)) value = _memoryPrefs[key];
if (value == null) value = _sharedPrefs.getDouble(key);
if (value == null) value = defaultValue;
return value;
}
// Remove user data before log-out etc.
Future<void> removeUserData() async {
await _sharedPrefs.remove(userKey);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment