getBlogs method
getBlogs This function will return all Blog
.
We need to pass listof BlogIds
.
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.
Implementation
Future<List<BlogsModel>> getBlogs({required List<String> blogIds}) async {
List<BlogsModel> blogsModels = [];
for (int i = 0; i < blogIds.length; i++) {
BlogsModel blogsModel = BlogsModel(error: 'API Call Failed');
try {
final res = await http.get(
Uri.parse('$baseUrlBlog/${blogIds[i]}?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:
blogsModel = BlogsModel.fromJson(res.body);
blogsModel = blogsModel
.copyWith(pages: BlogPage.fromJson(blogsModel.pages!.toJson()))
.copyWith(posts: BlogPost.fromJson(blogsModel.posts!.toJson()))
.copyWith(
locale: BlogLocale.fromJson(blogsModel.locale!.toJson()))
.copyWith(error: null);
break;
default:
/// we got some error so it;s update in [Error] column
blogsModel = BlogsModel(error: res.body);
}
} catch (e) {
/// we got some error so it;s update in [Error] column
blogsModel = BlogsModel(error: e.toString());
}
blogsModels.add(blogsModel);
}
return blogsModels;
}