数据在使用getX提供程序时未在产品页面上更新。

huangapple go评论74阅读模式
英文:

data is not updated on product page with getX provider

问题

我正在尝试在我的应用程序中设置getX提供程序,以从Firebase检索产品数据。
但是,当我打开新的产品页面时,数据没有更新,我看到之前产品的数据。
我对getX还不太了解,我想知道我是否做得对,或者是否漏掉了什么。

ProductController.dart

class ProductController extends GetxController {
  Rx<Product?> selectedProduct = Rx<Product?>(null);
  
  // 通过其ID获取单个产品的方法
  void fetchProductByID(String category, String productID) {
    _services.userRef.doc(_services.getUserID())
        .collection('Categories')
        .doc(category)
        .collection('products')
        .doc(productID)
        .get()
        .then((snapshot) {
      if (snapshot.exists) {
        var product = Product.fromDocumentSnapshot(snapshot);
        selectedProduct.value = product;
      } else {
        selectedProduct.value = null;
      }
    });
  }
}

main.dart中声明控制器

class MyBindings extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<UserController>(() => UserController());
    Get.put<ProductController>(ProductController());
  }
}

ProductPage

final ProductController _productController = Get.find();

@override
void initState() {
  super.initState();
  _productController.fetchProductByID(widget.catID, widget.docProdId);
}

Widget build(BuildContext context) {
  Product? product = _productController.selectedProduct.value;
  return Scaffold(
    body: product != null ? SingleChildScrollView(
      child: ClipOval(
        child: SizedBox.fromSize(
          size: const Size.fromRadius(82),
          child: Image.network != null
            ? Image.network(product.image, fit: BoxFit.cover)
            : const Image(
                fit: BoxFit.fill,
                image: AssetImage('assets/profile_placeholder.jpeg')),
        ),
        child: Column(
          children: [
            ProductCardLayout(chiave: 'Category'.tr, valore: product.name),
            const SizedBox(height: 20),
            ProductCardLayout(chiave: 'Stock'.tr, valore: product.stock),
            const SizedBox(height: 20),
            ProductCardLayout(chiave: 'Minimum Stock'.tr, valore: product.minStock),
            const SizedBox(height: 20),
            ProductCardLayout(chiave: 'Expire Date'.tr, valore: '12/05/2024'),  // : Container();
          ],
        ),
      ),
    ) : Container(),
  );
}

请注意,我没有翻译代码部分,只翻译了注释和文本。

英文:

I am trying to set the getX provider in my app to retrieve products data from firebase.
But when I open a new product page, data it's not updated and I see previous product's data.
I am still new to getX, and I wonder if I did all right, or I am missing something.

ProductController.dart

  class ProductController extends GetxController {
   Rx&lt;Product?&gt; selectedProduct = Rx&lt;Product?&gt;(null);
// Method to fetch a single product by its ID
  void fetchProductByID(String category, String productID) {
    _services.userRef.doc(_services.getUserID())
        .collection(&#39;Categories&#39;)
        .doc(category)
        .collection(&#39;products&#39;)
        .doc(productID)
        .get()
        .then((snapshot) {
      if (snapshot.exists) {
        var product = Product.fromDocumentSnapshot(snapshot);
        selectedProduct.value = product;
      } else {
        selectedProduct.value = null;
      }
    });
  }

Declared controller on main.dart

 class MyBindings extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut&lt;UserController&gt;(() =&gt; UserController());
    Get.put&lt;ProductController&gt;( ProductController() );
  }

ProductPage

     final ProductController _productController = Get.find();

    @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _productController.fetchProductByID(widget.catID, widget.docProdId);
  }
 Widget build(BuildContext context) {
    Product? product = _productController.selectedProduct.value;
    return Scaffold(
      
      body: product != null ? SingleChildScrollView(
                child: ClipOval(
                    child: SizedBox.fromSize(
                     size: const Size.fromRadius(82),
                     child: Image.network != null
                      ? Image.network( product.image, fit: BoxFit.cover)
                      : const Image(
                          fit: BoxFit.fill,
                          image: AssetImage(&#39;assets/profile_placeholder.jpeg&#39;)),
             
                child: Column(
                  children: [
                    ProductCardLayout(chiave: &#39;Category&#39;.tr, valore:product.name,),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: &#39;Stock&#39;.tr, valore: product.stock,),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: &#39;Minimum Stock&#39;.tr, valore:product.minStock),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: &#39;Expire Date&#39;.tr, valore: &#39;12/05/2024&#39;,),  : Container();

答案1

得分: 0

看起来你没有监听所选产品的更改。你可以使用 Obx 来实现这个。例如:

 Widget build(BuildContext context) {
    return Obx((){
      Product? product = _productController.selectedProduct.value;
      return Scaffold(
        
        body: product != null ? SingleChildScrollView(
                child: ClipOval(
                    child: SizedBox.fromSize(
                     size: const Size.fromRadius(82),
                     child: Image.network != null
                      ? Image.network( product.image, fit: BoxFit.cover)
                      : const Image(
                          fit: BoxFit.fill,
                          image: AssetImage('assets/profile_placeholder.jpeg')),
             
                child: Column(
                  children: [
                    ProductCardLayout(chiave: 'Category'.tr, valore:product.name,),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: 'Stock'.tr, valore: product.stock,),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: 'Minimum Stock'.tr, valore:product.minStock),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: 'Expire Date'.tr, valore: '12/05/2024',),
英文:

I looks like you are not listening to changes to the selectedProduct. To do this you can use an Obx. For your example:

 Widget build(BuildContext context) {
    return Obx((){
      Product? product = _productController.selectedProduct.value;
      return Scaffold(
        
        body: product != null ? SingleChildScrollView(
                child: ClipOval(
                    child: SizedBox.fromSize(
                     size: const Size.fromRadius(82),
                     child: Image.network != null
                      ? Image.network( product.image, fit: BoxFit.cover)
                      : const Image(
                          fit: BoxFit.fill,
                          image: AssetImage(&#39;assets/profile_placeholder.jpeg&#39;)),
             
                child: Column(
                  children: [
                    ProductCardLayout(chiave: &#39;Category&#39;.tr, valore:product.name,),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: &#39;Stock&#39;.tr, valore: product.stock,),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: &#39;Minimum Stock&#39;.tr, valore:product.minStock),
                    const SizedBox(height: 20),
                    ProductCardLayout(chiave: &#39;Expire Date&#39;.tr, valore: &#39;12/05/2024&#39;,),

</details>



huangapple
  • 本文由 发表于 2023年7月3日 17:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定