Reputation: 1747
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
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
Reputation: 14033
Since the method setListString
of the SharedPreference
takes a List of String.
TO SAVE
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
};
}
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();
// encode the Map which gives a String as a result
String jsonObjectString = jsonEncode(mapObject);
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
);
Json String
from the List
// get the json string from the List
String jsonObjectString = paymentStringList[2]; // using index 2 as an example
Json String
to a Map
by decoding it // convert the json string gotten to a Map object
Map mapObject = jsonDecode(jsonObjectString);
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