getAllBlogs method

Future<List<BlogsModel>> getAllBlogs({
  1. required List<String> blogId,
  2. required String apiKey,
})

getAllBlogs This function will return all Blog.

We need to pass listof BlogId and ApiKey.

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>> getAllBlogs(
    {required List<String> blogId, required String apiKey}) async {
  List<BlogsModel> blogsModels = [];

  for (int i = 0; i < blogId.length; i++) {
    BlogsModel blogsModel = BlogsModel(error: 'API Call Failed');
    try {
      final res = await http.get(
        Uri.parse('$blogApiUrlByid/${blogId[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;
}