英文:
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<Product?> selectedProduct = Rx<Product?>(null);
// Method to fetch a single product by its 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;
}
});
}
Declared controller on 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() {
// 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('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();
答案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('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',),
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论