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

标签 flutter dart sharedpreferences

我有以下类(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/

    相关文章:

    flutter - Flutter 中等效的 Android onResume() 方法

    flutter - Flutter Bloc-按钮未触发状态更改

    java - SwitchPreference的图形没有改变

    android - 保存 ProgressBar 的当前状态

    api - 如何设置一个基本 URL 以及在哪里为 api 调用在 flutter dio 中声明它?

    Flutter 如何创建分割圆合并图像

    dart - 使用Dart:IO Process类运行jar文件?

    java - 如何通过共享首选项保存菜单项可见性状态?

    flutter - flutter 错误 : <asynchronous suspension>

    flutter - 如何在 flutter 中使用下拉选择创建表单模式