arbis
arbis

Reputation: 1950

Firestore calculate values from documents

I have created a Future which prints out all of the fields from my firestore collection 'products' documents, but this collection is one of many of the same name, as seen below, and therefore I am printing out all of the values of each of the collections documents' fields. I want to add the 'productPrice' for each document that is in it's own collection 'products'

First Products Collection:

enter image description here

Second Products Collection:

enter image description here

My function is listening to all of the 'products' collections. When I try to print the productPrice field values for each of their documents, I get them all printed out, as seen below in the console. I need to add the productPrice values of 29.33 + 39.69 together to make 69.02 but I don't know how to do that without also adding the unrelated document that has the value of 149.99

  final FirebaseAuth _auth = FirebaseAuth.instance;
  final FirebaseFirestore _cloudStore = FirebaseFirestore.instance;

  List<OrderModel> orders = [];
  List<ProductModel> orderProductList = [];


Future startListening() async {
    User currentUser = _auth.currentUser;

    if(currentUser != null){
      _cloudStore
          .collection("orders")
          .doc(currentUser.email)
          .collection("orders")
          .snapshots()
          .listen((event) {

        orders = event.docs.map((value){
          return OrderModel(
            value.data()["name"], 
            value.data()["date"], 
            value.data()["dateTime"], 
            value.data()["status"],
            value.data()["productImage"],
          );
        }).toList();


        for(var i = 0; i < orders.length; i++){

          if(currentUser != null){
          _cloudStore
              .collection("orders")
              .doc(currentUser.email)
              .collection("orders")
              .doc(orders[i].dateTime)
              .collection("products")
              .snapshots()
              .listen((event){

                orderProductList = event.docs.map((value){

                  return ProductModel(
                    value.data()['productName'],
                    value.data()['productQuantity'],
                    value.data()['productPrice'],
                    value.data()['productImage'],
                  );
                  
                }).toList();

                print('separate');

                event.docs.forEach((value){
                  var prices = value.data()['productPrice'];
                  print(prices);
                });
                
                notifyListeners();

              });
          }
    }

        
        notifyListeners();

      });
    } else {
      return null;
    }
  }

My current output in the console:

Restarted application in 881ms.
Reloaded 0 of 983 libraries in 32ms.
flutter: separate
flutter: 149.99
flutter: separate
flutter: 29.33
flutter: 39.69

Upvotes: 2

Views: 875

Answers (1)

jpheine
jpheine

Reputation: 226

If I understand the question correctly, It should work if you change your code like that:

            double sum = 0.0;

            event.docs.forEach((value){
              var prices = value.data()['productPrice'];
              sum = sum + prices;
              print(prices);
            });

            print(sum);

You can just add that after your print('separate'); statement.

Upvotes: 1

Related Questions