flutter - 如何在 Flutter dart 中使用共享首选项保存和读取类对象列表?
问题描述
我有以下课程:
class Payment {
String date;
String time;
String code;
String toPay;
String payed;
String left;
Payment({ this.date, this.code, this.toPay, this.payed, this.left, this.time });
}
在我的 Flutter App 中,我应该保存并读取使用列表payments
并将shared preferences
该date
属性用作key
_ReadPayment(String date) async {
final prefs = await SharedPreferences.getInstance();
final key = date;
// here how i get the list of payment like getPaymentList instead of getStringList
final value = prefs.getStringList(key) ?? null;
}
_SavePayment(String date, List<Payment> list) async {
final prefs = await SharedPreferences.getInstance();
final key = date;
// here how i set Payment list instead of setStringList
prefs.setStringList(key,list);
}
解决方案
由于该方法setListString
需要SharedPreference
一个字符串列表。
保存
toMap()
在您的Payment
类中创建一个将Payment
对象转换为Map
.
class Payment {
String date;
String time;
String code;
String toPay;
String payed;
String left;
Payment({
this.date,
this.code,
this.toPay,
this.payed,
this.left,
this.time,
});
// method to convert Payment to a Map
Map<String, dynamic> toMap() => {
'date': date,
'time': time,
....// do the rest for other members
};
}
- 将您的
Payment
实例转换为Map<String, dynamic>
// payment instance
Payment payment = Payment();
// convert the payment instance to a Map
Map<String, dynamic> mapObject = payment.toMap();
- 编码从步骤 1获得的 `Map<String, dynamic> 的值
// encode the Map which gives a String as a result
String jsonObjectString = jsonEncode(mapObject);
- 编码结果是
String
您可以添加到List
可以传递给SharedPreference
setListString
方法的 a 。
// list to store the payment objects as Strings
List<String> paymentStringList = [];
// add the value to your List<String>
paymentStringList.add(jsonObjectString);
读书
fromMap()
1.在您的Payment
类中创建一个工厂构造函数,将 a 转换Map
为Payment
对象。
class Payment {
String date;
String time;
String code;
String toPay;
String payed;
String left;
Payment({
this.date,
this.code,
this.toPay,
this.payed,
this.left,
this.time,
});
// factory constructor to convert Map to a Payment
factory Payment.fromMap(Map<String, dynamic> map) => Payment(
date: map['date'],
time: map['time'],
.... // other members here
);
- 从
Json String
_List
// get the json string from the List
String jsonObjectString = paymentStringList[2]; // using index 2 as an example
- 通过解码将其转换
Json String
为aMap
// convert the json string gotten to a Map object
Map mapObject = jsonDecode(jsonObjectString);
- 使用类的
fromMap
构造函数Payment
将其转换Map
为Payment
对象
// use the fromMap constructor to convert the Map to a Payment object
Payment payment = Payment.fromMap(mapObject);
// print the members of the payment class
print(payment.time);
print(payment.date);
推荐阅读
- php - Move millions of files to root of S3
- hibernate - Hibernate JPA 2.1 在 createQuery() 上非常慢
- javascript - 动态画布、“移动线”Css 和/或 SVG
- android - 带有导航抽屉的工具栏上的Android“返回”操作
- php - 跨多个 AD 域的 LDAP PHP 身份验证
- python - 如何打开命令提示符以及使用 python 脚本运行的命令?
- node.js - 标记脚本没有 pakage.json
- spring - 在过滤器中获取请求标头
- r - 检查失败:Loss::CheckLabel(y) 标签必须在 [0,1] 中才能进行逻辑回归
- python - 航点追随者从类实例中获取空航点集合