前言
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)用到網(wǎng)絡(luò)請(qǐng)求,在flutter框架中,dio框架非常不錯(cuò),所以今天的文章在dio的基礎(chǔ)上搭建一套網(wǎng)絡(luò)持久化框架,那么在flutter項(xiàng)目中如何搭建一套,高可用性,維護(hù)性較高的dio公共請(qǐng)求參數(shù)框架呢?
搭建前夕準(zhǔn)備
一、基本認(rèn)知
- 要持久化那么必然要有存儲(chǔ)設(shè)備
- 持久化的數(shù)據(jù)在app啟動(dòng)后要即使填充到項(xiàng)目中
- 由于項(xiàng)目中網(wǎng)絡(luò)請(qǐng)求地址繁多,類型不同,需要持久化的位置不同
二、基于基本認(rèn)知來(lái)找合適的工具&必要點(diǎn)
2.1 持久化的工具:
我的推薦
- mmkv
- share_prefresence
今天主要用講解mmkv版本
2.2必要點(diǎn):
dio攔截器
攔截器是搭建這套持久化的關(guān)鍵
三、準(zhǔn)備好如上技能,我們來(lái)搭建這套持久化網(wǎng)絡(luò)框架吧
1、首先要知道有幾種類型的公共
//請(qǐng)求中url后追加公共請(qǐng)求
static const int urlPresistent = 1;
//請(qǐng)求頭中追加公共請(qǐng)求
static const int headerPresistent = 2;
//全部都追加公共請(qǐng)求
static const int allPresistent = 3;
2、構(gòu)建緩存參數(shù)(為了快速獲取)
static Map<String,Map<String, String?>> headerPersistent = Map();
static Map<String,Map<String, String?>> urlPersistent = Map();
3、構(gòu)建mmkv存儲(chǔ)結(jié)構(gòu)(加密存儲(chǔ))
static MMKV _store(String baseUrl, int type) => MMKVStore.sysSafeMMKV(name: '${SysConfig.sysPersistent}${baseUrl}_${type.toString()}');
4、構(gòu)建基本函數(shù)
單健值對(duì)存儲(chǔ)
static void setPersistent(String baseUrl,String key,String? value,{int type = allPresistent}){
if (type == allPresistent || type == headerPresistent) {
if (!headerPersistent.containsKey(baseUrl)) {
headerPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = headerPersistent[baseUrl]!;
keyMap[key] = value;
_store(baseUrl, headerPresistent).encodeString(key, value??"");
}
if (type == allPresistent || type == urlPresistent) {
if (!urlPersistent.containsKey(baseUrl)) {
urlPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = urlPersistent[baseUrl]!;
keyMap[key] = value;
_store(baseUrl, urlPresistent).encodeString(key, value??"");
}
}
多健值對(duì)存儲(chǔ)
static void setPersistentMap(String baseUrl,Map<String, String?> map,{int type = allPresistent}){
if (type == allPresistent || type == headerPresistent) {
if (!headerPersistent.containsKey(baseUrl)) {
headerPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = headerPersistent[baseUrl]!;
keyMap.addAll(map);
keyMap.forEach((key, value) {
_store(baseUrl, headerPresistent).encodeString(key, value??"");
});
}
if (type == allPresistent || type == urlPresistent) {
if (!urlPersistent.containsKey(baseUrl)) {
urlPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = urlPersistent[baseUrl]!;
keyMap.addAll(map);
keyMap.forEach((key, value) {
_store(baseUrl, urlPresistent).encodeString(key, value??"");
});
}
}
參數(shù)獲?。?/p>
static Map<String, String?>? getPersistent(String baseUrl, {int type = allPresistent}) {
Map<String, String?>? map;
if (type == allPresistent || type == headerPresistent) {
Map<String, String?>? headerMap;
if (headerPersistent.containsKey(baseUrl)) {
headerMap = headerPersistent[baseUrl];
} else {
headerMap = null;
}
if (headerMap != null) {
if (map == null) {
map = Map();
}
map.addAll(headerMap);
}
}
if (type == allPresistent || type == urlPresistent) {
Map<String, String?>? urlMap;
if (urlPersistent.containsKey(baseUrl)) {
urlMap = urlPersistent[baseUrl];
} else {
urlMap = null;
}
if (urlMap != null) {
if (map == null) {
map = Map();
}
map.addAll(urlMap);
}
}
return map;
}
刷新當(dāng)前緩存(應(yīng)用啟動(dòng)刷新)
static Map<String, String?> _all(String baseurl, int type) {
var mmkv= _store(baseurl, type);
var keys = mmkv.allKeys;
var map = Map<String, String?>();
keys.forEach((element) {
var value = mmkv.decodeString(element);
map[element] = value;
});
return map;
}
static void flushPersistent(String baseurl, {int type = allPresistent}) {
if (type == allPresistent || type == headerPresistent) {
var map = _all(baseurl, headerPresistent);
headerPersistent[baseurl]?.clear();
if (!headerPersistent.containsKey(baseurl)) {
headerPersistent[baseurl] = Map<String, String?>();
}
var keyMap = headerPersistent[baseurl]!;
keyMap.addAll(map);
}
if (type == allPresistent || type == urlPresistent) {
var map = _all(baseurl, urlPresistent);
urlPersistent[baseurl]?.clear();
if (!urlPersistent.containsKey(baseurl)) {
urlPersistent[baseurl] = Map<String, String?>();
}
var keyMap = urlPersistent[baseurl]!;
keyMap.addAll(map);
}
}
退出登陸移除持久化
static void removeAllPersistent(String baseurl, {int type = allPresistent}) {
if (type == allPresistent || type == headerPresistent) {
headerPersistent[baseurl]?.clear();
_store(baseurl, headerPresistent).clearAll();
}
if (type == allPresistent || type == urlPresistent) {
urlPersistent[baseurl]?.clear();
_store(baseurl, urlPresistent).clearAll();
}
}
攔截器實(shí)現(xiàn)(dio請(qǐng)求攔截管理)
class PresistentInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
var urlPersitents = HttpPersistent.getPersistent(options.baseUrl,type: HttpPersistent.urlPresistent);
var headerPersitents = HttpPersistent.getPersistent(options.baseUrl,
type: HttpPersistent.headerPresistent);
headerPersitents?.forEach((key, value) {
options.headers[key] = value;
});
urlPersitents?.forEach((key, value) {
options.queryParameters[key] = value;
});
super.onRequest(options, handler);
}
}
四、整體代碼&事件調(diào)用邏輯
整體代碼
class HttpPersistent{
static const int urlPresistent = 1;
static const int headerPresistent = 2;
static const int allPresistent = 3;
static MMKV _store(String baseUrl, int type) => MMKVStore.sysSafeMMKV(name: '${SysConfig.sysPersistent}${baseUrl}_${type.toString()}');
static Map<String,Map<String, String?>> headerPersistent = Map();
static Map<String,Map<String, String?>> urlPersistent = Map();
static void setPersistent(String baseUrl,String key,String? value,{int type = allPresistent}){
if (type == allPresistent || type == headerPresistent) {
if (!headerPersistent.containsKey(baseUrl)) {
headerPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = headerPersistent[baseUrl]!;
keyMap[key] = value;
_store(baseUrl, headerPresistent).encodeString(key, value??"");
}
if (type == allPresistent || type == urlPresistent) {
if (!urlPersistent.containsKey(baseUrl)) {
urlPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = urlPersistent[baseUrl]!;
keyMap[key] = value;
_store(baseUrl, urlPresistent).encodeString(key, value??"");
}
}
static void setPersistentMap(String baseUrl,Map<String, String?> map,{int type = allPresistent}){
if (type == allPresistent || type == headerPresistent) {
if (!headerPersistent.containsKey(baseUrl)) {
headerPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = headerPersistent[baseUrl]!;
keyMap.addAll(map);
keyMap.forEach((key, value) {
_store(baseUrl, headerPresistent).encodeString(key, value??"");
});
}
if (type == allPresistent || type == urlPresistent) {
if (!urlPersistent.containsKey(baseUrl)) {
urlPersistent[baseUrl] = Map<String, String?>();
}
var keyMap = urlPersistent[baseUrl]!;
keyMap.addAll(map);
keyMap.forEach((key, value) {
_store(baseUrl, urlPresistent).encodeString(key, value??"");
});
}
}
static Map<String, String?>? getPersistent(String baseUrl, {int type = allPresistent}) {
Map<String, String?>? map;
if (type == allPresistent || type == headerPresistent) {
Map<String, String?>? headerMap;
if (headerPersistent.containsKey(baseUrl)) {
headerMap = headerPersistent[baseUrl];
} else {
headerMap = null;
}
if (headerMap != null) {
if (map == null) {
map = Map();
}
map.addAll(headerMap);
}
}
if (type == allPresistent || type == urlPresistent) {
Map<String, String?>? urlMap;
if (urlPersistent.containsKey(baseUrl)) {
urlMap = urlPersistent[baseUrl];
} else {
urlMap = null;
}
if (urlMap != null) {
if (map == null) {
map = Map();
}
map.addAll(urlMap);
}
}
return map;
}
static Map<String, String?> _all(String baseurl, int type) {
var mmkv= _store(baseurl, type);
var keys = mmkv.allKeys;
var map = Map<String, String?>();
keys.forEach((element) {
var value = mmkv.decodeString(element);
map[element] = value;
});
return map;
}
static void flushPersistent(String baseurl, {int type = allPresistent}) {
if (type == allPresistent || type == headerPresistent) {
var map = _all(baseurl, headerPresistent);
headerPersistent[baseurl]?.clear();
if (!headerPersistent.containsKey(baseurl)) {
headerPersistent[baseurl] = Map<String, String?>();
}
var keyMap = headerPersistent[baseurl]!;
keyMap.addAll(map);
}
if (type == allPresistent || type == urlPresistent) {
var map = _all(baseurl, urlPresistent);
urlPersistent[baseurl]?.clear();
if (!urlPersistent.containsKey(baseurl)) {
urlPersistent[baseurl] = Map<String, String?>();
}
var keyMap = urlPersistent[baseurl]!;
keyMap.addAll(map);
}
}
static void removeAllPersistent(String baseurl, {int type = allPresistent}) {
if (type == allPresistent || type == headerPresistent) {
headerPersistent[baseurl]?.clear();
_store(baseurl, headerPresistent).clearAll();
}
if (type == allPresistent || type == urlPresistent) {
urlPersistent[baseurl]?.clear();
_store(baseurl, urlPresistent).clearAll();
}
}
}
class PresistentInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
var urlPersitents = HttpPersistent.getPersistent(options.baseUrl,type: HttpPersistent.urlPresistent);
var headerPersitents = HttpPersistent.getPersistent(options.baseUrl,
type: HttpPersistent.headerPresistent);
headerPersitents?.forEach((key, value) {
options.headers[key] = value;
});
urlPersitents?.forEach((key, value) {
options.queryParameters[key] = value;
});
super.onRequest(options, handler);
}
}
1、登陸后,調(diào)用存儲(chǔ)
HttpPersistent.setPersistent("http://www.baidu.com","token","123",HttpPersistent.headerPresistent)
2、退出登陸后,調(diào)用移除
HttpPersistent.removeAllPersistent("http://www.baidu.com",,type: HttpPersistent.headerPresistent);
3、應(yīng)用啟動(dòng)后刷新緩存
HttpPersistent.flushPersistent("http://www.baidu.com",
type: HttpPersistent.headerPresistent);
五、大功告成
如上就構(gòu)建出一套可靠性高,維護(hù)性高的網(wǎng)絡(luò)持久化框架
更多flutter教程請(qǐng)關(guān)注我的IMGeek:https://www.imgeek.org/people/33692