gpt4 book ai didi

flutter - 如何使用Flutter dart中的共享首选项保存和读取类对象的列表?

转载 作者:行者123 更新时间:2023-12-03 04:12:47 27 4
gpt4 key购买 nike

我有以下类(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 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);
}

最佳答案

由于setListStringSharedPreference方法采用字符串列表。
要保存

  • 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 的值

  •  // encode the Map which gives a String as a result
    String jsonObjectString = jsonEncode(mapObject);
  • 编码后的结果是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.在 fromMap()类中创建工厂构造函数 Payment,将 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,将其转换为 Map
     // 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);

    关于flutter - 如何使用Flutter dart中的共享首选项保存和读取类对象的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63863854/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com