preface

In the development process, we often use network requests. Dio framework is very good in Flutter framework, so today’s article builds a set of network persistence framework based on DIO. Then how to build a set of dio common request parameter framework with high availability and maintainability in Flutter project?

Preparation before setting up

1. Basic cognition

  1. To persist, you have to have a storage device
  2. Persistent data is populated into the project after the app starts
  3. Due to the various network request addresses and different types in the project, the location of persistence is different

Second, based on basic cognition to find appropriate tools & essential points

2.1 Persistence Tools:

I recommend

  1. mmkv
  2. share_prefresence

Today I will mainly explain the MMKV version

2.2 Essential Points:

Dio interceptor

Interceptors are the key to building this set of persistence

Three, ready for the above skills, let’s build this persistent network framework

The first thing to know is that there are several types of public

Static const int urlPresistent = 1; Static const int headerPresistent = 2; Static const int allPresistent = 3;Copy the code

2. Build cache parameters (for quick retrieval)

static Map<String,Map<String, String? >> headerPersistent = Map(); static Map<String,Map<String, String? >> urlPersistent = Map();Copy the code

3. Build MMKV storage structure (encrypted storage)

static MMKV _store(String baseUrl, int type) => MMKVStore.sysSafeMMKV(name: '${SysConfig.sysPersistent}${baseUrl}_${type.toString()}');
Copy the code

4. Build basic functions

Single-value pair storage

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??""); }}Copy the code

Multi-key pair storage

static void setPersistentMap(String baseUrl,Map<String, String? > map,{inttype = 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??""); }); }}Copy the code

Parameter acquisition:

static Map<String, String? >? getPersistent(String baseUrl, {inttype= 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;
  }
Copy the code

Flush the current cache (apply startup refresh)

static Map<String, String? > _all(String baseurl, inttype) {
    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); }}Copy the code

Log out and remove persistence

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();
    }
  }
Copy the code

Interceptor implementation (DIO Request Interception Management)

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); }}Copy the code

Overall code & event call logic

The overall code


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,{inttype = 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,{inttype = 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, {inttype= 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); }}returnmap; } static Map<String, String? > _all(String baseurl, inttype) {
    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, {inttype = 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); }}Copy the code

1, after landing, call storage HttpPersistent setPersistent (” www.baidu.com “, “token”, “123”, HttpPersistent. HeaderPresistent)

2, logged out after the call to remove HttpPersistent. RemoveAllPersistent (” www.baidu.com “, and type: HttpPersistent headerPresistent);

3, application startup after refresh cache HttpPersistent. FlushPersistent (” www.baidu.com “, type: HttpPersistent. HeaderPresistent);

Five, done

As mentioned above, a set of network persistence framework with high reliability and high maintainability is constructed

More pay attention to my IMGeek:www.imgeek.org/people/3369 flutter tutorial…