-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedPreferencesHandler.dart
164 lines (134 loc) · 3.94 KB
/
sharedPreferencesHandler.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'dart:async';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
/// SharedPreferences Util.
class SharedPrefHandler {
static SharedPrefHandler _singleton;
static SharedPreferences _prefs;
static Future<SharedPrefHandler> getInstance() async {
if (_singleton == null) {
// keep local instance till it is fully initialized.
var singleton = SharedPrefHandler._();
await singleton._init();
_singleton = singleton;
}
return _singleton;
}
SharedPrefHandler._();
Future _init() async {
_prefs = await SharedPreferences.getInstance();
}
/// put object.
static Future<bool> putObject(String key, Object value) {
return _prefs?.setString(key, json.encode(value));
}
/// get obj.
static T getObj<T>(String key, T f(Map v), {T defValue}) {
Map map = getObject(key);
return map == null ? defValue : f(map);
}
/// get object.
static Map getObject(String key) {
String _data = _prefs.getString(key);
return (_data == null || _data.isEmpty) ? null : json.decode(_data);
}
/// put object list.
static Future<bool> putObjectList(String key, List<Object> list) {
List<String> _dataList = list.map((value) {
return json.encode(value);
}).toList();
return _prefs?.setStringList(key, _dataList);
}
/// get obj list.
static List<T> getObjList<T>(String key, T f(Map v), {List<T> defValue = const []}) {
List<Map> dataList = getObjectList(key);
List<T> list = dataList?.map((value) {
return f(value);
})?.toList();
return list ?? defValue;
}
/// get object list.
static List<Map> getObjectList(String key) {
List<String> dataLis = _prefs?.getStringList(key);
return dataLis?.map((value) {
Map _dataMap = json.decode(value);
return _dataMap;
})?.toList();
}
/// get string.
static String getString(String key, {String defValue = ''}) {
return _prefs?.getString(key) ?? defValue;
}
/// put string.
static Future<bool> putString(String key, String value) {
return _prefs?.setString(key, value);
}
/// get bool.
static bool getBool(String key, {bool defValue = false}) {
return _prefs?.getBool(key) ?? defValue;
}
/// put bool.
static Future<bool> putBool(String key, bool value) {
return _prefs?.setBool(key, value);
}
/// get int.
static int getInt(String key, {int defValue = 0}) {
return _prefs?.getInt(key) ?? defValue;
}
/// put int.
static Future<bool> putInt(String key, int value) {
return _prefs?.setInt(key, value);
}
/// get double.
static double getDouble(String key, {double defValue = 0.0}) {
return _prefs?.getDouble(key) ?? defValue;
}
/// put double.
static Future<bool> putDouble(String key, double value) {
return _prefs.setDouble(key, value);
}
/// get string list.
static List<String> getStringList(String key, {List<String> defValue = const []}) {
return _prefs?.getStringList(key) ?? defValue;
}
/// put string list.
static Future<bool> putStringList(String key, List<String> value) {
return _prefs?.setStringList(key, value);
}
/// get dynamic.
static dynamic getDynamic(String key, {Object defValue}) {
return _prefs?.get(key) ?? defValue;
}
/// have key.
static bool haveKey(String key) {
return _prefs?.getKeys()?.contains(key);
}
/// contains Key.
static bool containsKey(String key) {
return _prefs?.containsKey(key);
}
/// get keys.
static Set<String> getKeys() {
return _prefs?.getKeys();
}
/// remove.
static Future<bool> remove(String key) {
return _prefs?.remove(key);
}
/// clear.
static Future<bool> clear() {
return _prefs?.clear();
}
/// Fetches the latest values from the host platform.
static Future<void> reload() {
return _prefs?.reload();
}
///Sp is initialized.
static bool isInitialized() {
return _prefs != null;
}
/// get Sp.
static SharedPreferences getSp() {
return _prefs;
}
}