I created these two classes to save a list in sharedpreferences.
My questions:
- how to access the list and add to it? I tried
Segments.segmentsbut no good. - how do I convert the list tojson using the class?
- how to retrieve the items inside the list.
I saw so many examples and videos, but I still don't get it.
class Segment {
final String chapter;
final String from;
final String to;
Segment({this.chapter, this.from, this.to});
factory Segment.fromJson(Map<String, dynamic> json) {
return Segment(
chapter: json['chapter'],
from: json['from'],
to: json['to'],
);
}
Map<String, dynamic> toJson() {
return {
'chapter': chapter,
'from': from,
'to': to,
};
}
}
&
class Segments {
List<Segment> segments;
Segments({this.segments});
Segments.fromJson(Map<String, dynamic> json) {
if (json['segments'] != null) {
segments = [];
json['segments'].forEach((v) {
segments.add(new Segment.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.segments != null) {
data['segments'] = this.segments.map((v) => v.toJson()).toList();
}
return data;
}
}