使用
使用相對(duì)來說比較簡(jiǎn)單
存儲(chǔ):
GetStorage().write('your_key', your_value);
讀取:
GetStorage().read('your_key');
問題
存儲(chǔ):
存儲(chǔ)的時(shí)候 我們持久化的值 若是 系統(tǒng)的基礎(chǔ)數(shù)據(jù)類型即 bool、String、int或是List馏谨、Map這些集合類型,且集合中也都是系統(tǒng)提供的數(shù)據(jù)類型附迷,完全沒有問題惧互,
但是有時(shí)候 我們灰直接存儲(chǔ)我們自定義的模型數(shù)據(jù),比如
HomeTabItemModel(
this.title,
this.image,
);
GetStorage().write('home_tab', itemModel);
這種也是可以直接存儲(chǔ)的 但是 需要注意 itemModel 需要實(shí)現(xiàn)toJson() 方法喇伯,否則存儲(chǔ)失敗 或是 下次獲取不到
原因是持久化時(shí)會(huì)調(diào)用json.encode()方法喊儡,該方法會(huì)調(diào)用非系統(tǒng)類的toJson()方法
這里需要注意 write()方法 寫入分為兩部分 一部分是內(nèi)存中的數(shù)據(jù)(寫入自定義模型,內(nèi)存中就是我們自定義的模型)稻据;另一部分是磁盤中的數(shù)據(jù)艾猜,該數(shù)據(jù)是json序列化后的數(shù)據(jù) 與 自定義模型的toJson()對(duì)應(yīng),所以此時(shí) 我們磁盤和內(nèi)存中的數(shù)據(jù)類型是不同的
///存儲(chǔ)
Future<void> write(String key, dynamic value) async {
// 內(nèi)存
writeInMemory(key, value);
// final _encoded = json.encode(value);
// await _concrete.write(key, json.decode(_encoded));
// 磁盤
return _tryFlush();
}
void writeInMemory(String key, dynamic value) {
_concrete.write(key, value);
}
內(nèi)存
void write(String key, dynamic value) {
subject
..value[key] = value
..changeValue(key, value);
//return _writeToStorage(subject.value);
}
磁盤
Future<void> _writeToStorage(Map<String, dynamic> data) async {
localStorage.update(fileName, (val) => json.encode(subject.value),
ifAbsent: () => json.encode(subject.value));
}
/// If [toEncodable] is omitted, it defaults to a function that returns the
/// result of calling `.toJson()` on the unencodable object.
String encode(Object? value, {Object? toEncodable(dynamic object)?}) {
toEncodable ??= _toEncodable;
if (toEncodable == null) return encoder.convert(value);
return JsonEncoder(toEncodable).convert(value);
}
讀取:
讀取分類兩種情況
1匆赃、系統(tǒng)類型的讀取淤毛,這種不管什么時(shí)間讀取 讀取到的都是一樣的都是我們存儲(chǔ)的數(shù)據(jù)類型
GetStorage().read<bool>('your_key');
GetStorage().read<String>('your_key');
...
2、自定義類型存儲(chǔ)后的讀取
這種形式的讀取分為兩種情況
1》存儲(chǔ)后 待下次冷啟動(dòng)才會(huì)調(diào)用read方法進(jìn)行讀取算柳,這種讀取到的是已經(jīng)經(jīng)過json序列化后的Map或是List數(shù)據(jù)低淡,原因在存儲(chǔ)的地方已經(jīng)說明
Iterable? json = GetStorage().read('your_key');
if (json != null) {
List<ItemModel> list = List<ItemModel>.from(json.map((e) => ItemModel.fromJson(e)));
if (list.isNotEmpty) {
tabs(list);
}
}
2》存儲(chǔ)后 還會(huì)在某個(gè)時(shí)刻讀取,這種情況讀取到的直接是我們自定的類型瞬项,此時(shí)讀取到的是內(nèi)存中經(jīng)過存儲(chǔ)更新后的數(shù)據(jù)蔗蹋,這種情況就會(huì)導(dǎo)致讀取到的數(shù)據(jù)類型與磁盤中不同,最好讀取方法這么寫
void readFromCache() {
Map<String, dynamic> map = Map.from(GetStorage().read("your_key") ?? {});
List<ItemModel> models = [];
for (var item in map.entries) {
ItemModel itemModel;
if (item.value is Map) {
itemModel = ItemModel.fromJson(item.value);
} else {
itemModel = item.value;
}
models.add(itemModel);
}
}
總結(jié)
根據(jù)上述問題 建議在存儲(chǔ)時(shí)盡量不使用我們自定義的數(shù)據(jù)類型直接存儲(chǔ)囱淋,存儲(chǔ)前要對(duì)數(shù)據(jù)進(jìn)行序列化猪杭,存儲(chǔ)系統(tǒng)提供的數(shù)據(jù)類型,這樣可保證 內(nèi)存和 磁盤中的數(shù)據(jù)一致绎橘,獲取時(shí)不容易出錯(cuò)