You don't need to add any libraries. You just have to implement fromJson and toJson functions in your object.
Example:
class LinkItem {
final String name;
final String url;
LinkItem({this.name, this.url});
LinkItem.fromJson(Map<String, dynamic> json)
: name = json['n'],
url = json['u'];
Map<String, dynamic> toJson() {
return {
'n': name,
'u': url,
};
}
}
Then you can call jsonEncode:
List<LinkItem> list = await getUserLinks();
list.add(linkItem);
String json = jsonEncode(list);
Result:
[{"n":"Google","u":"https://www.google.com/"},{"n":"Test","u":"https://www.test.com/"},{"n":"etc","u":"etc"}]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…