1.6 kv 离线存储
kv 什么意思
key value 的数据格式 ,典型代表 json、Map、 redis
https://www.json.org/json-en.html
实现步骤:
第 1 步:安装插件 shared_preferences
1
| flutter pub add shared_preferences
|
第 2 步:storage 类
lib/common/utils/storage.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import 'package:shared_preferences/shared_preferences.dart';
class Storage { static final Storage _instance = Storage._internal(); factory Storage() => _instance; late final SharedPreferences _prefs;
Storage._internal();
Future<void> init() async { _prefs = await SharedPreferences.getInstance(); }
Future<bool> setString(String key, String value) async { return await _prefs.setString(key, value); }
Future<bool> setBool(String key, bool value) async { return await _prefs.setBool(key, value); }
Future<bool> setList(String key, List<String> value) async { return await _prefs.setStringList(key, value); }
String getString(String key) { return _prefs.getString(key) ?? ''; }
bool getBool(String key) { return _prefs.getBool(key) ?? false; }
List<String> getList(String key) { return _prefs.getStringList(key) ?? []; }
Future<bool> remove(String key) async { return await _prefs.remove(key); } }
|
最后:global 初始
1 2 3 4 5 6
| class Global { static Future<void> init() async { ...
await Storage().init();
|
提交代码到 git