【问题标题】:how to save and read a list of class object using shared preferences in Flutter dart?如何在 Flutter dart 中使用共享首选项保存和读取类对象列表?
【发布时间】:2023-03-22 16:26:01
【问题描述】:

我有以下课程:

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);
}

【问题讨论】:

    标签: flutter dart sharedpreferences


    【解决方案1】:

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

    保存

    1. Payment 类中创建一个方法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&lt;String, dynamic&gt;
     // payment instance 
     Payment payment = Payment();
     // convert the payment instance to a Map
     Map<String, dynamic> mapObject = payment.toMap();
    
    1. 步骤 1 获得的 `Map 的值进行编码
     // encode the Map which gives a String as a result
     String jsonObjectString = jsonEncode(mapObject);
    
    1. 编码后的结果是一个String,您可以将其添加到一个List 中,该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.在Payment 类中创建工厂构造函数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 获取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 类的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);
    

    【讨论】:

    • 非常感谢,看来是我需要的,我试试看告诉你
    【解决方案2】:

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

    然后当你阅读它时,你只需反序列化它。

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2021-08-16
      • 2021-10-02
      • 2017-12-02
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      相关资源
      最近更新 更多