import 'package:cheminova/utils/api_urls.dart'; import '../models/product_model1.dart'; import '../utils/common_api_service.dart'; import '../utils/show_snackbar.dart'; class ProductService { // Method to fetch products based on the page number and optional category Future?> getProduct(int page, {String? category}) async { try { String url; // Determine the URL based on the presence of a category filter if (category != null && category.isNotEmpty) { url = "${ApiUrls.getallProductUrl}?page=$page&category=$category"; } else { url = "${ApiUrls.getallProductUrl}?page=$page"; // URL without category filter } // Make the API call using the common API service final response = await commonApiService>( method: "GET", url: url, fromJson: (json) { if (json['products'] != null) { // If the 'products' key exists, map it to a list of Product objects final List products = (json['products'] as List) .map((productJson) => Product.fromJson(productJson as Map)) .toList(); return products; } else { return []; } }, ); return response; } catch (e) { // showSnackbar(e.toString()); return null; } } // Method to fetch product categories Future>?> getCategory() async { try { // Make the API call to fetch categories final response = await commonApiService>>( method: "GET", url: ApiUrls.getCategoryUrl, fromJson: (json) { if (json['categories'] != null) { final List> category = (json['categories'] as List) .map((productJson) => productJson as Map) .toList(); return category; // Return the list of categories } else { return []; // Return an empty list if no categories are found } }, ); return response; } catch (e) { showSnackbar(e.toString()); print("Error: $e"); } return null; } }