import 'dart:convert'; import 'package:cheminova/controller/rd_get_single_service.dart'; import 'package:cheminova/models/single_get_order_model.dart'; import 'package:get/get.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../utils/show_snackbar.dart'; class RdSingleOrderController extends GetxController { var isLoading = true.obs; // Tracks the loading state var productRDOrderSingleList = [].obs; // List of products Future fetchRDSingleOrderProduct(String id) async { try { SharedPreferences prefs = await SharedPreferences.getInstance(); String? token = prefs.getString('token'); // Check if the token exists before proceeding if (token == null || token.isEmpty) { print("Token is missing"); throw Exception("Token is missing"); } print("Login token: $token"); isLoading(true); // Start loading // Check if the list has any elements if (productRDOrderSingleList.isEmpty) { print("No orders found."); showSnackbar("No orders found."); return; // Early exit if no orders are available } // Fetch the product using the API call, passing the token final response = await GetSingleProductService().GetsingleOrder(token, id); if (response != null) { // Assign the fetched products to the observable list (uncomment this if needed) // productRDOrderSingleList.assignAll(response); print("Fetched order details: $response"); } else { print("Response is null"); showSnackbar("Response is null"); } } catch (e) { // Print the error for debugging print("Error: $e"); showSnackbar("Error: $e"); } finally { isLoading(false); // End loading } } }