简体   繁体   English

flutter - flutter 提供商未更新用户界面

[英]flutter - flutter provider not updating the ui

I am working on an e-commerce project.我正在从事电子商务项目。 In that project, I am using the provider package to manage states in flutter. In development mode provider updates the UI but When I build the apk using flutter build apk --release it does not update the UI.在那个项目中,我使用provider package来管理 flutter 中的状态。在开发模式下,提供者更新 UI,但是当我使用flutter build apk --release ,它不会更新 UI。

I don't understand why is it not updating the UI instantly.我不明白为什么它不立即更新用户界面。

cart provider购物车供应商


class CartProvider with ChangeNotifier {
  int _counter = 0;
  int get counter => _counter;
  double _totalPrice = 0.0;
  double get totalPrice => _totalPrice;
 
  int getCounter() {
    final box = Boxes.getData();
    print(box.length);
    notifyListeners();
    return box.length;
  }
}

adding product to hive db as well as updating the cart count using provider将产品添加到 hive 数据库以及使用提供商更新购物车数量

 var data = ProductModel(
                                name: widget.productTitle.toString(),
                                price: totalController.text,
                                quantity: widget.productqty.toString(),
                                unit: widget.productunit[0]['unit'],
                                agentId: widget.agentId,
                                menuId: widget.menuId,
                                productId: widget.productId,
                                showunitlist: widget.showunitlist.toString(),
                                serviceid: widget.serviceid,
                                unitlist: list,
                                originalprice: '');

                            cart.addTotalPrice(
                                double.parse(totalController.text.toString()));
                            cart.addCounter();
                            final box = Boxes.getData();
                            box.add(data);
                            data.save();

appbar应用栏

 appBar: AppBar(
        title: const Text("Cart"),
        actions: [
          Row(
            children: [
              InkWell(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const CartList()));
                },
                child: Center(
                  child: Badge(
                    badgeContent: Consumer(
                      builder: ((context, value, child) {
                        return Text(
                          cart.getCounter().toString(),
                          style: const TextStyle(color: Colors.white),
                        );
                      }),
                    ),
                    child: const Icon(Icons.shopping_cart),
                  ),
                ),
              ),

Connect your Provider class with the Consumer widget as you see below:将您的 Provider class 连接到 Consumer 小部件,如下所示:

Consumer<CartProvider>(
                  builder: ((context, cartProvider , child) {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM