Asmoun
Asmoun

Reputation: 1747

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

i have the following 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 });
    }

in my Flutter App i'm supposed to save and read a list of payments using shared preferences and use the date attribute as the 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);
}

Upvotes: 0

Views: 982

Answers (2)

TSR
TSR

Reputation: 20386

Shared preference does not support saving Dart object directly. What you can do is serialize your object ( Payment ) into a json string (we call this map in dart) then saved it as string .

Then when you read it, you just deserialize it.

You can achieve that using a library I recommend Json serializer but you can also do that easily manually.

Give it a try and let me know how it goes in the comment.

Upvotes: 1

timilehinjegede
timilehinjegede

Reputation: 14033

Since the method setListString of the SharedPreference takes a List of String.

TO SAVE

  1. Create a method toMap() in your Payment class which convert a Payment object to a 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. Convert your Payment instance to a Map<String, dynamic>
 // payment instance 
 Payment payment = Payment();
 // convert the payment instance to a Map
 Map<String, dynamic> mapObject = payment.toMap();
  1. Encode the value of the `Map<String, dynamic> gotten from the step 1
 // encode the Map which gives a String as a result
 String jsonObjectString = jsonEncode(mapObject);
  1. The encoded result is a String which you can add to a List that can be passed to the SharedPreference setListString method.
  // list to store the payment objects as Strings
  List<String> paymentStringList = [];
  // add the value to your List<String>
   paymentStringList.add(jsonObjectString);

TO READ

1.Create a factory constructor fromMap() in your Payment class which convert a Map to a Payment object.

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. Get the Json String from the List
 // get the json string from the List
 String jsonObjectString = paymentStringList[2]; // using index 2 as an example
  1. Convert the Json String to a Map by decoding it
 // convert the json string gotten to a Map object
 Map mapObject = jsonDecode(jsonObjectString);
  1. Use the fromMap constructor of your Payment class to convert the Map to a Payment object
  // 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);

Upvotes: 2

Related Questions