Home > front end >  How to find the total value(sum) of a column in flutter and firebase
How to find the total value(sum) of a column in flutter and firebase

Time:10-05

I am trying to get the sum of a column that I have added in flutter. This is the partial code of the place where I am rendering the data as a list form firebase.

            Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: SizedBox(
                      height: 250,
                      width: 330,
                      child: ListView(
                        padding: EdgeInsets.all(25),
                        children: [
                          StreamBuilder<QuerySnapshot>(
                            stream: FirebaseFirestore.instance.collection('topup').snapshots(),
                            builder: (context, snapshot){
                              if(snapshot.hasData){
                                return Expanded(
                                  child: SizedBox(
                                    height: 400,
                                    child: ListView.builder(
                                      itemCount: snapshot.data!.docs.length,
                                        itemBuilder: (context, index){
                                          QueryDocumentSnapshot pay = snapshot.data!.docs[index];
                                          return Container(
                                            width: 100,
                                            child: Card(
                                              child: Padding(
                                                padding: EdgeInsets.all(15),
                                                child: Row(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  crossAxisAlignment: CrossAxisAlignment.center,
                                                  children: [
                                                    Column(
                                                      crossAxisAlignment: CrossAxisAlignment.start,
                                                      children: [
                                                        Row(
                                                          children: [
                                                            Column(
                                                              children: [
                                                                Text("Date"),
                                                                Text(" "),
                                                                Text("Amount"),
                                                              ],
                                                            ),
                                                            Text("  "),
                                                            Column(
                                                              children: [
                                                                Text(pay['date']),
                                                                Text(" "),
                                                                Text(pay['payment']   ".00"),
                                                              ],
                                                            ),
                                                          ],
                                                        ),

                                                      ],
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                          );
                                        },
                                    ),
                                  ),
                                );
                              }else{
                                return Center(
                                  child: CircularProgressIndicator(),
                                );
                              }
                            }
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),

In here I need to get the sum of 'payment', I am trying this from yesterday but nothing seems to be correct.

This is how I am saving the 'payment' data.

                               Padding(
                                        padding: EdgeInsets.all(8.0),
                                        child: TextFormField(
                                          maxLength: 4,
                                          keyboardType:
                                          TextInputType.number,
                                          inputFormatters: [
                                            FilteringTextInputFormatter
                                                .digitsOnly,
                                          ],
                                          decoration: InputDecoration(
                                            hintText: "500",
                                            labelText: "Amount",
                                            labelStyle: TextStyle(
                                                color: Colors.black),
                                            border: OutlineInputBorder(
                                              borderRadius:
                                              BorderRadius.circular(10),
                                            ),
                                          ),
                                          controller: topamount,
                                          validator: (value) {
                                            if (value == null || value.isEmpty) {
                                              return "Please enter an amount";
                                            }
                                          },
                                        ),
                                      ),

And this is how I am did the create method for the button. (Once I select the button it will save data)

topup() async {
try {
  FirebaseFirestore.instance.collection('topup').doc().set({
    "fullName": topfullName.text,
    "card": topcard.text,
    "date": topdate.text,
    "cvc": "***",
    "payment": topamount.text,
    "cardNumber": cardno.text,
  });
} catch (e) {
  print(e);
}}

Please help me how I can do the sum part. And is the way I saved the payment correct?

CodePudding user response:

double sum = 0.0;

...
child: ListView.builder(
       itemCount: snapshot.data!.docs.length,
       itemBuilder: (context, index){
       QueryDocumentSnapshot pay = snapshot.data!.docs[index];
       sum  = double.parse(pay['payment']); //Add this line
       return Container(...
  • Related