2)getCategory wise product api integration 3)Category wise product filter added
61 lines
1.7 KiB
Dart
61 lines
1.7 KiB
Dart
import '../utils/common_api_service.dart';
|
|
import '../utils/show_snackbar.dart';
|
|
|
|
class ProductService {
|
|
Future<List<Map<String, dynamic>>?> getProduct(int page, {String? category}) async {
|
|
try {
|
|
String url;
|
|
if (category != null && category.isNotEmpty) {
|
|
url = "/api/product/getAll/user?page=$page&category=$category";
|
|
} else {
|
|
url = "/api/product/getAll/user?page=$page"; // URL without category filter
|
|
}
|
|
|
|
final response = await commonApiService<List<Map<String, dynamic>>>(
|
|
method: "GET",
|
|
url: url,
|
|
fromJson: (json) {
|
|
if (json['products'] != null) {
|
|
final List<Map<String, dynamic>> products = (json['products'] as List)
|
|
.map((productJson) => productJson as Map<String, dynamic>)
|
|
.toList();
|
|
return products;
|
|
} else {
|
|
return [];
|
|
}
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
showSnackbar(e.toString());
|
|
//print("Error: $e");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
Future<List<Map<String, dynamic>>?> getCategory() async {
|
|
try {
|
|
final response = await commonApiService<List<Map<String, dynamic>>>(
|
|
method: "GET",
|
|
url: "/api/category/getCategories",
|
|
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;
|
|
}
|
|
}
|