如何在 Flutter dart 中使用共享首选项保存和读取 class 对象列表?

how to save and read a list of class object using shared preferences in Flutter dart?

我有以下 class :

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 应用程序中,我应该使用 shared preferences 保存和读取 payments 的列表,并将 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);
}

由于 SharedPreference 的方法 setListString 采用字符串列表。

保存

  1. 在您的 Payment class 中创建方法 toMap(),将 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
  };
}

  1. 将您的 Payment 实例转换为 Map<String, dynamic>
 // payment instance 
 Payment payment = Payment();
 // convert the payment instance to a Map
 Map<String, dynamic> mapObject = payment.toMap();
  1. 对从第 1 步
  2. 获得的 `Map 的值进行编码
 // encode the Map which gives a String as a result
 String jsonObjectString = jsonEncode(mapObject);
  1. 编码结果是一个 String,您可以将其添加到 List,然后再传递给 SharedPreference setListString 方法。
  // list to store the payment objects as Strings
  List<String> paymentStringList = [];
  // add the value to your List<String>
   paymentStringList.add(jsonObjectString);

阅读

1.Create Payment class 中的工厂构造函数 fromMap()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
    );

  1. List
  2. 中获取 Json String
 // get the json string from the List
 String jsonObjectString = paymentStringList[2]; // using index 2 as an example
  1. 通过解码将 Json String 转换为 Map
 // convert the json string gotten to a Map object
 Map mapObject = jsonDecode(jsonObjectString);
  1. 使用 Payment class 的 fromMap 构造函数将 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);

共享首选项不支持直接保存Dart对象。 您可以做的是将您的对象 ( Payment ) 序列化为 json 字符串(我们在 dart 中称此映射)然后将其保存为字符串 .

然后你读的时候反序列化就好了

您可以使用我推荐的库 Json 序列化程序来实现这一点,但您也可以轻松地手动完成。

试一试,在评论中告诉我效果如何。