In this Project App is receiving posts from a WP REST API as JSON, it works fine, but I need to convert it to an Item Model (for caching and DB): so I created an Item-Model, now how can I implement it in my code, which gets String. how can I Map my data in getPosts() so it returns a Post, not a String? please, Please show me the way in CODE.
// Function to get list of posts which is String
Future<String> getPosts() async {
var res = await http.get(Uri.encodeFull(apiUrl + "posts?_embed&per_page=10"),
headers: {"Accept": "application/json"});
setState(() {
var resBody = json.decode(res.body);
posts = resBody;
});
return "Success!";
}
Future builder to show a Loading indicator before loading posts:
body: FutureBuilder<List<String>>(
future: getPosts(),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? ListViewPosts(posts: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
Code for ItemModel
class Post {
int _id;
String _title;
String _content;
String _author;
String _date;
String _imgUrl;
Post(this._id, this._title, this._content, this._author, this._date,
[this._imgUrl]);
Post.withId(this._id, this._title, this._content, this._author, this._date,
[this._imgUrl]);
int get id => _id;
String get title => _title;
String get content => _content;
String get author => _author;
String get date => _date;
String get imgUrl => _imgUrl;
set title(String newTitle) {
this._title = newTitle;
}
set content(String newContent) {
this._content = newContent;
}
set author(String newAuthor) {
this._author = newAuthor;
}
set date(String newDate) {
this._date = newDate;
}
set imgUrl(String newImgUrl) {
this._imgUrl = newImgUrl;
}
//convert post to Map
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
if (id != null) {
map['id'] = _id;
}
map['title'] = _title;
map['content'] = _content;
map['author'] = _author;
map['date'] = _date;
map['imgurl'] = _imgUrl;
return map;
}
//Extract post from Map Object
Post.fromMapObject(Map<String, dynamic> map) {
this._id = map['id'];
this._title = map['title'];
this._content = map['content'];
this._author = map['author'];
this._date = map['date'];
this._imgUrl = map['imgurl'];
}
}
the JSON response
{
"id": 73331,
"date": "2018-11-24T19:00:21",
"date_gmt": "2018-11-24T19:00:21",
"guid": {
"rendered": "theUrl?p=73331"
},
"modified": "2018-11-24T19:00:21",
"modified_gmt": "2018-11-24T19:00:21",
"slug": "%d8%aa%db%8e%d8%b3%d8%aa-%d8%a8%d9%88-%d9%be%d9%87%e2%80%8c%da%95%db%8c-%d8%b3%d9%87%e2%80%8c%d8%b1%d9%87%e2%80%8c%d9%83%db%8c",
"status": "publish",
"type": "post",
"link": "theUrl/2018/11/24/%d8%aa%db%8e%d8%b3%d8%aa-%d8%a8%d9%88-%d9%be%d9%87%e2%80%8c%da%95%db%8c-%d8%b3%d9%87%e2%80%8c%d8%b1%d9%87%e2%80%8c%d9%83%db%8c/",
"title": {
"rendered": "تێست بو په\u200cڕی سه\u200cره\u200cكی"
},
"content": {
"rendered": "<p>تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی</p>\n<div class=\"likebtn_container\" style=\"\"><!-- LikeBtn.com BEGIN --><span class=\"likebtn-wrapper\" data-identifier=\"post_73331\" data-theme=\"large\" data-lang=\"ck\" data-ef_voting=\"buzz\" data-tooltip_enabled=\"false\" data-white_label=\"true\" data-rich_snippet=\"true\" data-popup_disabled=\"true\" data-style=\"\" data-unlike_allowed=\"\" data-show_copyright=\"\" data-item_url=\"theUrl/2018/11/24/%d8%aa%db%8e%d8%b3%d8%aa-%d8%a8%d9%88-%d9%be%d9%87%e2%80%8c%da%95%db%8c-%d8%b3%d9%87%e2%80%8c%d8%b1%d9%87%e2%80%8c%d9%83%db%8c/\" data-item_title=\"تێست بو په\u200cڕی سه\u200cره\u200cكی\" data-item_image=\"theUrl/wp-content/uploads/2018/11/IMG_5203-1024x620.jpg\" data-item_date=\"2018-11-24T19:00:21+00:00\" data-engine=\"WordPress\" data-plugin_v=\"2.6.11\" data-event_handler=\"likebtn_eh\" ></span><!-- LikeBtn.com END --></div>",
"protected": false
},
"excerpt": {
"rendered": "<p>تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی تێست بو په\u200cڕی سه\u200cره\u200cكی</p>\n<div class=\"likebtn_container\" style=\"\"><!-- LikeBtn.com BEGIN --><span class=\"likebtn-wrapper\" data-identifier=\"post_73331\" data-theme=\"large\" data-lang=\"ck\" data-ef_voting=\"buzz\" data-tooltip_enabled=\"false\" data-white_label=\"true\" data-rich_snippet=\"true\" data-popup_disabled=\"true\" data-style=\"\" data-unlike_allowed=\"\" data-show_copyright=\"\" data-item_url=\"theUrl/2018/11/24/%d8%aa%db%8e%d8%b3%d8%aa-%d8%a8%d9%88-%d9%be%d9%87%e2%80%8c%da%95%db%8c-%d8%b3%d9%87%e2%80%8c%d8%b1%d9%87%e2%80%8c%d9%83%db%8c/\" data-item_title=\"تێست بو په\u200cڕی سه\u200cره\u200cكی\" data-item_image=\"theUrl/wp-content/uploads/2018/11/IMG_5203-1024x620.jpg\" data-item_date=\"2018-11-24T19:00:21+00:00\" data-engine=\"WordPress\" data-plugin_v=\"2.6.11\" data-event_handler=\"likebtn_eh\" ></span><!-- LikeBtn.com END --></div>",
"protected": false
},
"author": 1,
"featured_media": 73332,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"template": "",
"format": "standard",
"meta": [],
"categories": [
1
],
"tags": [],
"acf": [],
"_links": {
"self": [
{
"href": "theUrl/wp-json/wp/v2/posts/73331"
}
],
"collection": [
{
"href": "theUrl/wp-json/wp/v2/posts"
}
],
"about": [
{
"href": "theUrl/wp-json/wp/v2/types/post"
}
],
"author": [
{
"embeddable": true,
"href": "theUrl/wp-json/wp/v2/users/1"
}
],
"replies": [
{
"embeddable": true,
"href": "theUrl/wp-json/wp/v2/comments?post=73331"
}
],
}

json_annotation. @Hooshyar there doesn't seem to be animgUrlfield in the JSON. Do you mean the img url given within thecontentorexcerptrendered?