getBlogBySlug method

Future<BlogPost> getBlogBySlug({
  1. required String slug,
  2. required int appId,
})

Get a single blog post by slug

Implementation

Future<BlogPost> getBlogBySlug({
  required String slug,
  required int appId,
}) async {
  try {
    final response = await _dio.get(
      '$baseUrl/blog-posts/$slug',
      queryParameters: {'language': Get.locale?.languageCode ?? 'en'},
    );

    if (response.statusCode == 200) {
      return BlogPost.fromJson(response.data['data'] as Map<String, dynamic>);
    } else if (response.statusCode == 404) {
      throw Exception('Blog post not found');
    } else {
      throw Exception('Failed to load blog');
    }
  } on DioException catch (e) {
    if (e.response?.statusCode == 404) {
      throw Exception('Blog post not found');
    }
    throw Exception('Error fetching blog: ${e.message}');
  } catch (e) {
    throw Exception('Error: $e');
  }
}