64 lines
1.8 KiB
Dart
64 lines
1.8 KiB
Dart
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 {
|
|
Future<List<Product>?> getProduct(int page, {String? category}) async {
|
|
try {
|
|
String url;
|
|
if (category != null && category.isNotEmpty) {
|
|
url = "ApiUrls.getProductUrl?page=$page&category=$category";
|
|
} else {
|
|
url = "/api/product/getAll/user?page=$page"; // URL without category filter
|
|
}
|
|
|
|
final response = await commonApiService<List<Product>>(
|
|
method: "GET",
|
|
url: url,
|
|
fromJson: (json) {
|
|
if (json['products'] != null) {
|
|
final List<Product> products = (json['products'] as List)
|
|
.map((productJson) => Product.fromJson(productJson as Map<String, dynamic>))
|
|
.toList();
|
|
return products;
|
|
} else {
|
|
return [];
|
|
}
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
// showSnackbar(e.toString());
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
Future<List<Map<String, dynamic>>?> getCategory() async {
|
|
try {
|
|
final response = await commonApiService<List<Map<String, dynamic>>>(
|
|
method: "GET",
|
|
url: ApiUrls.getCategoryUrl,
|
|
fromJson: (json) {
|
|
if (json['categories'] != null) {
|
|
final List<Map<String, dynamic>> category = (json['categories'] as List)
|
|
.map((productJson) => productJson as Map<String, dynamic>)
|
|
.toList();
|
|
return category;
|
|
} else {
|
|
return [];
|
|
}
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
showSnackbar(e.toString());
|
|
print("Error: $e");
|
|
}
|
|
return null;
|
|
}
|
|
}
|