0

I have 2 screens: ShowProductDetail and EditProductDetail.

The Product model

class Product {
  String? id;
  String? name;
  String? description;
  double? price;
  double? discount;
  int? originalStockCount;
  int? currentStockCount;
}

And the controller

class ProductController extends GetxController {
    var currentProduct = Product().obs
    
    // aditional methods to update, delete etc
}

First I see the product detail on ShowProductDetail, then click the edit button to display EditProductDetail. Do some editings, and the go back to ShowProductDetail

Thanks to Obx, all widgets displaying the product detail with automatically updated accordingly, e.g.

Obx(() => Text(controller.currentProduct.value.description!,
    style: TextStyle(fontSize: 15, color: Colors.purple))

But what if I want to access the recently updated values not on widget?

e.g

if (product.currentStockCount <= 10){
    // show alert "Stock is reaching 0"
}   

Assume product.currentStockCount on EditProductDetail is updated from 30 to 8. When I go back to ShowProductDetail, that part will not be executed, because product.currentStockCount still refers to its old value, which is 30.

How to fix this?

anta40
  • 6,511
  • 7
  • 46
  • 73
  • How are you updating `currentStockCount` and how are you navigating back to `ShowProductDetail`? It should reflect the new value when you go back but I'd need to see what you're doing to see what the problem is. – Loren.A Sep 29 '21 at 18:59
  • On *EditProductDetail*, an update API is called (not shown), which if success then returns the whole JSON of the updated product, like this: `Product updatedProduct = Product.fromJSON(....); controller.product.value = updatedProduct; controller.product.refresh()` (assume I update all the details, not just current stock count). Then I go back to `ShowProductDetail`, and thanks to `Obx`, the product name description, price etc displayed on `Text` are automatically updated. No problem here. The issue is doing something not related to widget, e.g the if block shown above. – anta40 Sep 30 '21 at 04:05
  • Well without actually seeing your code I can suggest if you're using `Get.back()` to navigate there directly with `Get.to(()=> ShowProductDetail ())` or `Get.toNamed(...)`. I just did a quick conditional check on an updated value on a previous page and it was only executed when I navigated with `Get.toNamed` – Loren.A Sep 30 '21 at 04:31

0 Answers0