getBlog method

Future<BlogsModel> getBlog({
  1. required String blogId,
})

getBlog This function will return Blog, Post and Page details.

We need to pass BlogId.

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<BlogsModel> getBlog({required String blogId}) async {
  BlogsModel blogsModel = BlogsModel(error: 'API Call Failed');
  try {
    final res = await http.get(
      Uri.parse('$baseUrlBlog/$blogId?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());
  }
  return blogsModel;
}