getBlogByID method

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

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

We need to pass 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<BlogsModel> getBlogByID(
    {required String blogId, required String apiKey}) async {
  BlogsModel blogsModel = BlogsModel(error: 'API Call Failed');
  try {
    final res = await http.get(
      Uri.parse('$blogApiUrlByid/$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;
}