demo 地址: https://github.com/iotjin/jh_flutter_demo
数据存储使用的是 flustars
存数据时先对key和value 进行加密处理,然后再存到本地,这样本地数据都是密文
aes加密工具类 JhEncryptUtils
JhStorageUtils
/**
* jh_storage_utils.dart
*
* Created by iotjin on 2020/05/07.
* description: aes 数据存储 ,封装第三方
*/
import 'dart:convert' as convert;
import 'package:flustars/flustars.dart';
import 'package:jh_flutter_demo/jh_common/utils/jh_encrypt_utils.dart';
// aes 加密存储
class JhStorageUtils {
//存 String
static Future<bool> saveString(String key, String value) {
key = JhEncryptUtils.aesEncrypt(key);
value = JhEncryptUtils.aesEncrypt(value);
return SpUtil.putString(key, value);
}
//取 String
static String getStringWithKey(String key) {
key = JhEncryptUtils.aesEncrypt(key);
var enValue = SpUtil.getString(key);
if (enValue.length > 0) {
return JhEncryptUtils.aesDecrypt(enValue);
}
return enValue;
}
//存 bool
static Future<bool> saveBool(String key, bool value) {
var newValue = value == true ? "TRUE" : "FALSE";
return saveString(key, newValue);
}
//取 bool
static bool getBoolWithKey(String key) {
var value = getStringWithKey(key);
return value == "TRUE" ? true : false;
}
//存 int
static Future<bool> saveInt(String key, int value) {
var newValue = value.toString();
return saveString(key, newValue);
}
//取 int
static int getIntWithKey(String key) {
var value = getStringWithKey(key);
value = value == '' ? '0' : value;
return int.parse(value);
}
//存 double
static Future<bool> saveDouble(String key, double value) {
var newValue = value.toString();
return saveString(key, newValue);
}
//取 double
static double getDoubleWithKey(String key) {
var value = getStringWithKey(key);
value = value == '' ? '0' : value;
return double.parse(value);
}
//存 Model
static Future<bool> saveModel(String key, Object model) {
String jsonString = convert.jsonEncode(model);
return saveString(key, jsonString);
}
//取 Model
static Map getModelWithKey(String key) {
var jsonString = getStringWithKey(key);
return (jsonString == null || jsonString.isEmpty)
? null
: convert.jsonDecode(jsonString);
}
//移除
static Future<bool> removeWithKey(String key) {
key = JhEncryptUtils.aesEncrypt(key);
return SpUtil.remove(key);
}
}
调用
print('-------------------本地加密存储----------------------');
JhStorageUtils.saveString("testStr", "这是测试本地加密存储的字符串");
var testStr = JhStorageUtils.getStringWithKey("testStr");
print('testStr : ${testStr}');
JhStorageUtils.saveBool('testBool', true);
var testBool = JhStorageUtils.getBoolWithKey('testBool');
print('testBool : ${testBool}');
JhStorageUtils.saveInt('testInt', 1111);
var testInt = JhStorageUtils.getIntWithKey('testInt');
print('testInt : ${testInt}');
JhStorageUtils.saveDouble('testDouble', 222.333354);
var testDouble = JhStorageUtils.getDoubleWithKey('testDouble');
print('testDouble : ${testDouble}');
var dic = {"a": "aaa", 'b': "bbb", "c": "ccc"};
// print('原始dic: ${dic}');
JhStorageUtils.saveModel("testDic", dic);
var testDic = JhStorageUtils.getModelWithKey('testDic');
print('testDic : ${testDic}');
打印
flutter: -------------------本地加密存储----------------------
flutter: testStr : 这是测试本地加密存储的字符串
flutter: testBool : true
flutter: testInt : 1111
flutter: testDouble : 222.333354
flutter: testDic : {a: aaa, b: bbb, c: ccc}
本地数据
![](https://img.haomeiwen.com/i12175332/0095b1ab463c79fd.png)
网友评论