Home > front end >  How to covert data retrieved from Firestore by using DocumentSnapshot to double?
How to covert data retrieved from Firestore by using DocumentSnapshot to double?

Time:06-08

Im trying to calculate the total price of items in the shopping cart which are retrieved from the Firestore using DocumentSnapshot. My trick is to sum up the prices as the ListView Builder complete the iteration, but i got the "type 'double' is not a subtype of type 'String'" error message.

This is the code to sum up the price in the shopping cart:

price = double.parse(documentSnapshot['price'])   price;

The initialization of the price

double price = 0;

The code for the shopping cart

Widget bodySection(BuildContext context) {
return StreamBuilder(
    stream: FirebaseFirestore.instance
        .collection('users')
        .doc(userid)
        .collection('cart')
        .snapshots(),
    builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
      if (streamSnapshot.hasData) {
        return Padding(
          padding: EdgeInsets.symmetric(
              horizontal: getProportionateScreenWidth(20)),
          child: ListView.builder(
              itemCount: streamSnapshot.data!.docs.length,
              itemBuilder: (context, index) {
                final DocumentSnapshot documentSnapshot =
                    streamSnapshot.data!.docs[index];
                    price = double.parse(documentSnapshot['price'])   price;
                String imgurl = documentSnapshot['imgUrl'];
                return Padding(
                    padding: EdgeInsets.symmetric(vertical: 10),
                    child: Dismissible(
                      key: Key(documentSnapshot['id']),
                      direction: DismissDirection.endToStart,
                      onDismissed: (direction) {
                        setState(() {
                        });
                      },
                      background: Container(
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        decoration: BoxDecoration(
                          color: Color(0xFFFFE6E6),
                          borderRadius: BorderRadius.circular(15),
                        ),
                        child: Row(
                          children: [
                            Spacer(),
                            Icon(Icons.delete),
                          ],
                        ),
                      ),
                      child: Container(
                        padding: EdgeInsets.all(
                                        getProportionateScreenWidth(8)),
                                    decoration: BoxDecoration(
                                      color: Colors.white ,
                                      borderRadius: BorderRadius.circular(15),  
                                    ),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 88,
                              child: AspectRatio(
                                aspectRatio: 0.88,
                                child: Container(
                                    padding: EdgeInsets.all(
                                        getProportionateScreenWidth(5)),
                                    decoration: BoxDecoration(
                                      color: Colors.black,
                                      borderRadius: BorderRadius.circular(10),
                                      image: DecorationImage(image: NetworkImage(imgurl), fit: BoxFit.cover)
                                    ),
                                    ),
                              ),
                            ),
                            SizedBox(width: 20),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  documentSnapshot['brand']   " "   documentSnapshot['name'], 
                                  style: TextStyle(
                                      color: Colors.black, fontSize: 16),
                                  maxLines: 2,
                                ),
                                SizedBox(height: 10),
                                Text.rich(
                                  TextSpan(
                                    text:
                                        "RM "   price.toString(),
                                    style: TextStyle(
                                        fontWeight: FontWeight.w600,
                                        color: kPrimaryColor),
                                  ),
                                )
                              ],
                            ),
                            Spacer(),
                            Column(
                              children: [
                                SizedBox(height: 30,),
                                Text(
                                      "x 1", 
                                      style: TextStyle(
                                          color: Colors.black, fontSize: 16),
                                    ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ));
              }
              ),
        );
      }
      return const Center(
        child: CircularProgressIndicator(),
      );
    });

}

CodePudding user response:

 price = double.parse(documentSnapshot['price'])   price;

to

price = documentSnapshot['price']   price;

i think your documentSnapshot['price'] is already returning double & double.parse(' ') takes string thats why its showing "type 'double' is not a subtype of type 'String'"

  • Related