getAllPostFromBlog method
getAllPostFromBlog This function will return all Post in the blog
We need to pass BlogId, IncludeComment, ApiKey and MaxResult Max page data
if the statusCode is 200 then the API call is Successful elase the will be updated in BlogsModel.error field.
if BlogsModel.error is null then your all is Successful.
Post Comment is included Replies.postComments so this function will take some time to get all post and Comments based on blog size.
Implementation
Future<PostModel> getAllPostFromBlog({
required String blogId,
required String apiKey,
bool includeComment = false,
int maxresult = 10,
}) async {
PostModel postModel = PostModel(error: 'API Call Failed', items: []);
List<PostItemModel>? items = [];
try {
final res = await http.get(Uri.parse(
'$blogApiUrlByid/$blogId/posts?maxResults=$maxresult&key=$apiKey'));
switch (res.statusCode) {
/// if the status code is 2000 then you API call is Successful and we got the datat from server
case 200:
if (jsonDecode(res.body)['items'] != null) {
for (int i = 0; i < jsonDecode(res.body)['items'].length; i++) {
PostItemModel postItemModel =
PostItemModel(error: 'API Call Failed');
List<PostComments>? postCommentslist = [];
postItemModel = PostItemModel.fromJson(
jsonEncode(jsonDecode(res.body)['items'][i]));
Response? res1;
try {
if (includeComment) {
res1 = await http.get(Uri.parse(
'${postItemModel.replies!.selfLink!}?key=$apiKey'));
if (jsonDecode(res1.body)['items'] != null) {
for (int k = 0;
k < jsonDecode(res1.body)['items'].length;
k++) {
PostComments postComments = PostComments.fromJson(
jsonEncode(jsonDecode(res1.body)['items'][k]));
postCommentslist.add(postComments);
}
postItemModel = PostItemModel.fromJson(
jsonEncode(jsonDecode(res.body)['items'][i]))
.copyWith(
replies: Replies(
postComments: postCommentslist,
selfLink: postItemModel.replies!.selfLink,
totalItems: postItemModel.replies!.totalItems,
));
} else {
postItemModel = PostItemModel.fromJson(
jsonEncode(jsonDecode(res.body)['items'][i]))
.copyWith(
replies: Replies(
postComments: [],
selfLink: postItemModel.replies!.selfLink,
totalItems: postItemModel.replies!.totalItems,
));
}
} else {
postItemModel = PostItemModel.fromJson(
jsonEncode(jsonDecode(res.body)['items'][i]))
.copyWith(
replies: Replies(
postComments: [],
selfLink: postItemModel.replies!.selfLink,
totalItems: postItemModel.replies!.totalItems,
),
);
}
} catch (e) {
//print(e.toString());
}
items.add(postItemModel);
}
postModel = PostModel(
items: items,
error: null,
etag: jsonDecode(res.body)['etag'],
kind: jsonDecode(res.body)['kind'],
);
} else {
/// it means no items in the Blog
postModel = PostModel(
items: [],
error: null,
etag: jsonDecode(res.body)['etag'],
kind: jsonDecode(res.body)['kind'],
);
}
break;
default:
/// we got some error so it;s update in [Error] column
postModel = PostModel(error: res.body, items: null);
}
} catch (e) {
/// we got some error so it;s update in [Error] column
postModel = PostModel(error: e.toString());
}
return postModel;
}