pd-android-app/lib/controller/product_service.dart
saritabirare 215877afc4 1)Order Place api Integration
2)Confirm Order api Integration
3)get Oder api Integration
2024-09-06 14:39:40 +05:30

62 lines
1.7 KiB
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 = "/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<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: "/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;
}
}