firebase 问题是增加数量、减少数量和产品价格

laawzig2  于 2023-02-16  发布在  其他
关注(0)|答案(1)|浏览(148)

当我按下一个按钮,其他按钮自动按下。我不知道的问题。第二个是与产品价格时,我控告的项目,价格成为100100,如果我有价格100请解决我的问题。
下面是我的代码
整数_数量= 1;流生成器

StreamBuilder(
        stream: FirebaseFirestore.instance.collection("Cart").where("id",isEqualTo:FirebaseAuth.instance.currentUser!.uid).snapshots(),
        builder:(BuildContext context,AsyncSnapshot<QuerySnapshot>snapshot){
          if (!snapshot.hasData) {
            return Center(child: LoadingAnimationWidget.staggeredDotsWave(color: Colors.red, size: 100));
          }
         return ListView.builder(
             itemCount: snapshot.data!.docs.length,
             itemBuilder: (itemBuilder,index){
               return Padding(
                 padding: const EdgeInsets.all(8.0),

产品价格
x一个一个一个一个x一个一个二个x

uoifb46i

uoifb46i1#

snapshot.data!.docs[index]["productprice"]中似乎有一个String值。您应该在乘法之前将String转换为doubleint
因此,您的代码应如下所示

Text(
           "${ double.parse(snapshot.data!.docs[index]["productprice"].toString()) * _quantity}",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),
    • 编辑日期:**

如果要管理多个项目,则引入一个字段final Map<String, int> quantity = <String, int>{};

StreamBuilder(
        stream: FirebaseFirestore.instance.collection("Cart").where("id",isEqualTo:FirebaseAuth.instance.currentUser!.uid).snapshots(),
        builder:(BuildContext context,AsyncSnapshot<QuerySnapshot>snapshot){
          if (!snapshot.hasData) {
            return Center(child: LoadingAnimationWidget.staggeredDotsWave(color: Colors.red, size: 100));
          }
         return ListView.builder(
             itemCount: snapshot.data!.docs.length,
             itemBuilder: (itemBuilder,index){
               final doc = snapshot.data!.docs[index];
               final _quantity = quantity[doc.id] ?? 0;
               return Padding(
                 padding: const EdgeInsets.all(8.0),
              

child: Card(
  child: Container(
    child: Row(
      children: [
            Row(
              children: [
fina;
                Text(
           "${doc["productprice"]*_quantity}",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),

                SizedBox(width: 16),
                IconButton(
                  icon: Icon(Icons.remove),
                  onPressed: (){
                      if (_quantity>1) {
                    setState(() {
                      quantity[doc.id] = _quatity - 1;
                    });
                   }
                  },
                ),
                SizedBox(width: 8),
                Text(
                  "$_quantity",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),
                SizedBox(width: 8),
                IconButton(
                  icon: Icon(Icons.add),
                  onPressed: (){
                    setState(() {
                      quantity[doc.id] = _quatity + 1;
                    });
                  },
                ),
              ],
            ),
          ],
        ),
      ],
    ),
  ),
)));

相关问题