getRecentBlogs method

Future<List<BlogPost>> getRecentBlogs({
  1. int limit = 5,
})

Get recent blog posts

Implementation

Future<List<BlogPost>> getRecentBlogs({int limit = 5}) async {
  try {
    final response = await _dio.get(
      '$baseUrl/blogs/recent',
      queryParameters: {'limit': limit},
    );

    if (response.statusCode == 200) {
      final List<dynamic> data = response.data['data'] as List;
      return data
          .map((item) => BlogPost.fromJson(item as Map<String, dynamic>))
          .toList();
    } else {
      throw Exception('Failed to load recent blogs');
    }
  } on DioException catch (e) {
    throw Exception('Error fetching recent blogs: ${e.message}');
  } catch (e) {
    throw Exception('Error: $e');
  }
}