rd-android-app/lib/controller/product_controller.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

68 lines
1.8 KiB
Dart

import 'package:cheminova/controller/product_service.dart';
import 'package:cheminova/models/product_model1.dart';
import 'package:get/get.dart';
class ProductController extends GetxController {
final ProductService productService = ProductService();
var products = <Product>[].obs;
var categories = <String>[].obs; // Holds the list of categories
var selectedCategory = Rxn<String>(); // Holds the selected category
int _currentPage = 1;
bool isLoading = false;
@override
void onInit() {
super.onInit();
getCategory();
getUser();
}
Future<void> getUser() async {
if (isLoading) return;
isLoading = true;
try {
final category = selectedCategory.value; // Get the selected category
final fetchedProducts = await productService.getProduct(
_currentPage,
category: category,
);
if (fetchedProducts != null) {
// Directly add the fetched products (which are of type List<Product>) to the list
products.addAll(fetchedProducts);
print("fetchedProducts ,$fetchedProducts");
}
} catch (e) {
print("Error fetching products: $e");
} finally {
isLoading = false;
update();
}
}
Future<void> getCategory() async {
try {
final fetchedCategories = await productService.getCategory();
if (fetchedCategories != null) {
categories.assignAll(fetchedCategories.map((category) => category['categoryName'] as String));
categories.insert(0, 'All'); // Add "All" option
}
} catch (e) {
print("Error fetching categories: $e");
}
}
void setCategory(String category) {
selectedCategory.value = category == 'All' ? null : category;
_currentPage = 1;
products.clear();
getUser();
}
void loadMoreProducts() {
_currentPage++;
getUser();
}
}