我正在Flutter中使用firebase创建一个杂货店应用程序,但无法将更新的购物车值显示到文本小部件

eaf3rand  于 2022-12-24  发布在  Flutter
关注(0)|答案(1)|浏览(78)

我正在Flutter中创建一个杂货店应用程序,但我无法将更新后的购物车值显示到文本小部件。
这是我的列表视图

var fire_storedb = FirebaseFirestore.instance.collection("vegetables").snapshots();

Container(
    margin: const EdgeInsets.only(top: 2),
    alignment: Alignment.center,
    child: StreamBuilder(
        stream: fire_storedb,
        builder: ((context, snapshot) {
          if (!snapshot.hasData) return const CircularProgressIndicator();
          return (ListView.builder(
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) {
              return (grocery_list(snapshot, index, context,values45));
            },
          ));
        })),
  ),

下面是我的growth_list函数,它是从ListView调用的........

Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                         children: [
        
                              InkWell(
                                  child: Icon(FontAwesomeIcons.minus),
                              onTap: () async {
                                String? grocery_id =
                                    snapshot.data?.docs[index].reference.id;
                                FirebaseFirestore.instance
                                    .collection("Cart")
                                    .where("grocery_id", isEqualTo: grocery_id)
                                    .get()
                                    .then((value) {
                                  value.docs.forEach((element) {
                                    FirebaseFirestore.instance
                                        .collection("Cart")
                                        .doc(element.id)
                                        .delete()
                                        .then((value) {
                                      print("Success!");
                                    });
                                  });
                                });
                              }),      //Inkwell for delete item from cart

                          VerticalDivider(width: 10,),    ////// Vertical Divider
                          

                          VerticalDivider(width: 10,),    ////// Vertical Divider
    
                          InkWell(
                            child: Icon(FontAwesomeIcons.plus),
                            onTap: () async {
                              SharedPreferences sharedPreferences =
                                  await SharedPreferences.getInstance();
                              var email =
                                  sharedPreferences.getString("email").toString();
                              String? docid =
                                  snapshot.data?.docs[index].reference.id;
    
                              Map<String, String?> data_to_save = {
                                "grocery_id": docid,
                                "quantity": "1",
                                "email": email,
                                "name": snapshot.data!.docs[index]['name'],
                                "price": snapshot.data!.docs[index]['price'],
                                "si": snapshot.data!.docs[index]['si'],
                                "image": snapshot.data!.docs[index]['image'],
                              };
                              var collectionRef = await FirebaseFirestore.instance
                                  .collection("Cart");
                              collectionRef.add(data_to_save);
                            },
                          ),        // Inkwell for add item to cart
                        ],
                      ),

我想把下面的代码之间的两个垂直分隔符作为一个文本宽显示没有项目添加到购物车。有人能帮忙吗。?我可以得到购物车的值在cart_value,但无法显示它的文本小部件。

FirebaseFirestore.instance.collection("Cart").get().then((value) {
          value.docs.forEach((element) {
            FirebaseFirestore.instance.collection("Cart").doc(element.id).get().then((value2) => {
              if(value2.data()!['grocery_id']==docid)
                cart_value = (value2.data()['quantity'])
            });
          });
        });

5uzkadbs

5uzkadbs1#

应该使用Future方法从Firestore获取数据,并返回cart_value的整数或双精度值,如下所示:

int cart_value = 0;
Future<int> cart() async {
  var cart = await FirebaseFirestore.instance.collection("Cart").get();
  for (var element in cart.docs) {
    FirebaseFirestore.instance.collection("Cart").doc(element.id).get().then((value2) => {
      if(value2.data()!['grocery_id']==docid){
        cart_value = (value2.data()!['quantity'])
      }

    });
  }
  return cart_value;
}

并将Future方法车放在FutureBuilder小部件的未来中:

FutureBuilder(
                future: cart(),
                builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
                  if (snapshot.hasData) {
    return Text(snapshot.data!.toString());
    }})

相关问题