Day 28 shared_preferences本机存放区

(一)介绍
Flutter推荐使用者使用shared_prederences储存资料,可以存取少量资料到手机的xml文件中,大量资料则采用SQLite行动装置资料库或放在云端。

(二)特性
1.为一个非同步、简单、持久化且以key_value形式储存的系统。
2.在Android端以SharedPreferences开发为基础。
3.在iOS端以NSUserDefaults开发为基础。

(三)在pubspec.yaml增加依赖
可以到https://pub.dev/packages/shared_preferences参考所需 版本。
https://ithelp.ithome.com.tw/upload/images/20211010/2014052477qPv1oD8N.png

(四)操作
•储存资料
SharedPreferences prefs = await SharedPreferences.getInstance();
+
prefs.setString(key,value); //字串
prefs.setBool(key,value); //布林
prefs.setDouble(key,value); //浮点数
prefs.setInt(key,value); //整数
prefs.setStringList(key,value); //字串列表
•读取资料
SharedPreferences prefs = await SharedPreferences.getInstance();
+
prefs.getString(key); //字串
prefs.getBool (key); //布林
prefs.getDouble (key); //浮点数
prefs.getInt (key); //整数
prefs.getStringList (key); //字串列表
•删除资料
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove(key);

(五)实作
程序码
https://ithelp.ithome.com.tw/upload/images/20211010/20140524S9QZGDesJ9.png
https://ithelp.ithome.com.tw/upload/images/20211010/20140524bxHxeV3xRA.png
https://ithelp.ithome.com.tw/upload/images/20211010/20140524fQbNNl2s58.png
执行画面
https://ithelp.ithome.com.tw/upload/images/20211010/20140524dAdoKM6BJR.png

今天先介绍到这边,明天将会介绍SQLite资料库,敬请期待。

参考资料
横跨Android及Apple的神话:用Dart语言神启Flutter大业8.1节
http://tw-hkt.blogspot.com/2019/08/flutter-sharedpreferences.html
https://ithelp.ithome.com.tw/articles/10227342
https://pub.dev/packages/shared_preferences


<<:  【Day 25】NumPy (2)

>>:  Day 25 解决Kali在Virtualbox上找不到无线网卡的问题

Day4- 整数与变数 和 型态转换

数字延伸如何使用将变数指定值进行运算,并将不同型态转成字串与浮点数。 整数与变数 结合算数运算子与赋...

【Day5】後端设定(前端的建立在晚一点)

因为某种神秘的原因(公司比较忙),所以目前前端的建立过程可能会明後天才会介绍 1.後端与前端的架构:...

ADV campaigns: why it is important to monitor conversions

In recent years, with the development of web marke...

【Day8】[资料结构]-伫列Queue-实作

伫列(Queue)建立的方法 enqueue: 尾端新增元素 dequeue: 从前端移除元素 pe...

[DAY24]Istio-Gateway

K8s除了自带的Ingress Gateway外,还可以透过Istio Ingress Gatewa...