Search code examples
flutterfirebasegoogle-cloud-firestorestream-buildericonbutton

The issue is with incriment quantity ,decriment quantity and product price


When I pressed one button The other buttons automatically pressed .I don't know the issue . And the second is with product price when i incriment the item the price become 100100 if i have price 100 Please solve my issues.

Here is my code

int _quantity=1; Stream builder

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),
              

Product price

child: Card(
  child: Container(
    child: Row(
      children: [
            Row(
              children: [
                Text(
           "${ snapshot.data!.docs[index]["productprice"]*_quantity}",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),

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

             });

        } ,

      ),

Solution

  • Looks you have a String value in snapshot.data!.docs[index]["productprice"]. You should convert String to double or int before multiplication.

    So you code should look as following

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

    Edited:

    If you want to manage with multiple items, then introduce one field 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;
                        });
                      },
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    )));