shared_preferences
源碼分析
實(shí)例化對(duì)象源碼分析
接下來我們來對(duì)shared_preferences
進(jìn)行分析,我們?cè)谑褂玫臅r(shí)候需要通過getInstance
實(shí)例化一個(gè)對(duì)象,接下來我們看下這里面它都做了什么操作泪喊。
靜態(tài)變量分析
我們先來看下它定義了三個(gè)靜態(tài)變量:
_prefix: 設(shè)置持久化數(shù)據(jù)和讀取持久化數(shù)據(jù)時(shí)統(tǒng)一設(shè)置前綴(flutter.)
_completer: 持久化數(shù)據(jù)異步通知,就是當(dāng)shared_preferences
實(shí)例化完成后通過completer.future
來返回結(jié)果
_manualDartRegistrationNeeded: 是否需要手動(dòng)注冊(cè)髓涯,因?yàn)樯婕暗?code>Linux袒啼、Windows
、Mac Os
的持久化數(shù)據(jù)時(shí)纬纪,是需要手動(dòng)進(jìn)行注冊(cè)的蚓再,默認(rèn)為true
static const String _prefix = 'flutter.';
static Completer<SharedPreferences>? _completer;
static bool _manualDartRegistrationNeeded = true;
getInstance()
源碼分析
當(dāng)我們獲取實(shí)例化對(duì)象時(shí)先判斷_completer
是否為空,如果不為空則直接返回它的的future
結(jié)果包各,否則它會(huì)實(shí)例化一個(gè)SharedPreferences
的Completer
對(duì)象摘仅,然后通過_getSharedPreferencesMap
來獲取持久化的map
對(duì)象,獲取到map
對(duì)象后问畅,通過completer.complete(SharedPreferences._(preferencesMap))
將Map
結(jié)果返回出去娃属,代碼如下:
static Future<SharedPreferences> getInstance() async {
if (_completer == null) {
final completer = Completer<SharedPreferences>();
try {
final Map<String, Object> preferencesMap =
await _getSharedPreferencesMap();
completer.complete(SharedPreferences._(preferencesMap));
} on Exception catch (e) {
// If there's an error, explicitly return the future with an error.
// then set the completer to null so we can retry.
completer.completeError(e);
final Future<SharedPreferences> sharedPrefsFuture = completer.future;
_completer = null;
return sharedPrefsFuture;
}
_completer = completer;
}
return _completer!.future;
}
_getSharedPreferencesMap()
源碼分析
在我們調(diào)用getInstance()
方法里,會(huì)調(diào)用_getSharedPreferencesMap()
來獲取持久化的Map數(shù)據(jù)护姆,我們接下來看看它是如何獲取的矾端,首先它通過_store.getAll()
就可以直接獲取到本地的所有持久化數(shù)據(jù),當(dāng)我們調(diào)用_store
時(shí)卵皂,它會(huì)判斷是否需要手動(dòng)注冊(cè)
不需要手動(dòng)注冊(cè)時(shí):
在iOS
秩铆、Android
等平臺(tái)中使用不需要手動(dòng)注冊(cè),所以它直接就返回的對(duì)應(yīng)的實(shí)例對(duì)象
需要手動(dòng)注冊(cè)時(shí):
先判斷是否是web
灯变,如果是就返回localStorage
殴玛,否則判斷是Linux
還是Windows
,然后根據(jù)平臺(tái)的不同返回其對(duì)應(yīng)的實(shí)例柒凉。
static Future<Map<String, Object>> _getSharedPreferencesMap() async {
final Map<String, Object> fromSystem = await _store.getAll();
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
for (String key in fromSystem.keys) {
assert(key.startsWith(_prefix));
preferencesMap[key.substring(_prefix.length)] = fromSystem[key]!;
}
return preferencesMap;
}
static SharedPreferencesStorePlatform get _store {
// TODO(egarciad): Remove once auto registration lands on Flutter stable.
// https://github.com/flutter/flutter/issues/81421.
if (_manualDartRegistrationNeeded) {
// Only do the initial registration if it hasn't already been overridden
// with a non-default instance.
if (!kIsWeb &&
SharedPreferencesStorePlatform.instance
is MethodChannelSharedPreferencesStore) {
if (Platform.isLinux) {
SharedPreferencesStorePlatform.instance = SharedPreferencesLinux();
} else if (Platform.isWindows) {
SharedPreferencesStorePlatform.instance = SharedPreferencesWindows();
}
}
_manualDartRegistrationNeeded = false;
}
return SharedPreferencesStorePlatform.instance;
}
_setValue()
源碼分析
不管我們是存儲(chǔ)什么內(nèi)容的數(shù)據(jù),最終都會(huì)調(diào)用_setValue()
來進(jìn)行存儲(chǔ)扰楼,
首先它會(huì)檢查存入的value
是否為空之碗,如果為空就拋出異常架忌,否則就用_prefix + key
來作為存入的key
值臭杰。
判斷存入的值是不是List<String>
,如果是先把value
通過toList()
方法轉(zhuǎn)換蔬咬,然后在存入鲤遥,否則直接存入,這步存入操作只是存入緩存中林艘,當(dāng)應(yīng)用程序退出時(shí)將消失
最后通過_store
來異步寫入到磁盤中
Future<bool> _setValue(String valueType, String key, Object value) {
ArgumentError.checkNotNull(value, 'value');
final String prefixedKey = '$_prefix$key';
if (value is List<String>) {
// Make a copy of the list so that later mutations won't propagate
_preferenceCache[key] = value.toList();
} else {
_preferenceCache[key] = value;
}
return _store.setValue(valueType, prefixedKey, value);
}
本篇主要講了shared_preferences
的源碼盖奈,下篇講講項(xiàng)目中使用shared_preferences
的封裝部分。