title: How to iterate through every document and sum properties in Firestore with Flutter
body: <p>I'm creating an app in which you put the amount of crypto you have and then with an API it retrieves the amount with live data.
  Thing is I have this ListView with all the cards for all the cryptos but I'd like a Container at the bottom of the screen with the total of all the amounts.
  I don't know how to create a function that goes through every &quot;Coins&quot; of a user and then retrieve the amount of the total.
  Thing is in Firestore I only have the quantity and then in my HomePage with the cards there's the function with the Api that calculates the amount in usd.
  Could you help me to create some function which does that?</p>
  <p>Here's my code of my HomePage view :</p>
  <pre><code>class HomeView extends StatefulWidget {
  const HomeView({ Key? key }) : super(key: key);
  
  @override
   _HomeViewState createState() =&gt; _HomeViewState();
   }
  
   class _HomeViewState extends State&lt;HomeView&gt; {
  
   double bitcoin = 0.0;
   double ethereum = 0.0;
   double tether = 0.0;
   double ultra = 0.0;
   double ternoa = 0.0;
   double dai = 0.0;
   double litecoin = 0.0;
   double cardano = 0.0;
   double stellar = 0.0;
   double tezos = 0.0;
   double elrond = 0.0;
   double dogecoin = 0.0;
   double solana = 0.0;
  
  
   @override
    initState() {
     updateValues();
    }
  
   updateValues() async {
  bitcoin = await getPrice(&quot;bitcoin&quot;);
  ethereum = await getPrice(&quot;ethereum&quot;);
  tether = await getPrice(&quot;tether&quot;);
  ultra  = await getPrice(&quot;ultra&quot;);
  ternoa = await getPrice(&quot;coin-capsule&quot;);
  dai = await getPrice(&quot;dai&quot;);
  litecoin = await getPrice(&quot;litecoin&quot;);
  cardano = await getPrice(&quot;cardano&quot;);
  stellar = await getPrice(&quot;stellar&quot;);
  tezos = await getPrice(&quot;tezos&quot;);
  elrond = await getPrice(&quot;elrond-erd-2&quot;);
  dogecoin = await getPrice(&quot;dogecoin&quot;);
  solana = await getPrice(&quot;solana&quot;);
  setState(() {});
  }
  
  @override
  Widget build(BuildContext context) {
  getValue(String id, double amount) {
    if (id == &quot;bitcoin&quot;) {
      return (bitcoin * amount).toStringAsFixed(2);
    } else if (id == &quot;ethereum&quot;) {
      return (ethereum * (amount)).toStringAsFixed(2);
    } else if (id == &quot;ultra&quot;){
      return(ultra*amount).toStringAsFixed(2);
    }else if(id == &quot;coin-capsule&quot;){
      return(ternoa*amount).toStringAsFixed(2);
    }else if (id == &quot;dai&quot;){
      return(dai*amount).toStringAsFixed(2);
    }else if (id == &quot;litecoin&quot;){
      return(litecoin*amount).toStringAsFixed(2);
    }else if (id == &quot;cardano&quot;){
      return(cardano*amount).toStringAsFixed(2);
    }else if (id == &quot;stellar&quot;){
      return(stellar*amount).toStringAsFixed(2);
    }else if (id == &quot;tezos&quot;){
      return(tezos*amount).toStringAsFixed(2);
    }else if (id == &quot;elrond-erd-2&quot;){
      return(elrond*amount).toStringAsFixed(2);
    }else if (id == &quot;dogecoin&quot;){
      return(dogecoin*amount).toStringAsFixed(2);
    }else if (id == &quot;solana&quot;){
      return(solana*amount).toStringAsFixed(2);
    }
    else {
      return (tether * amount).toStringAsFixed(2);
    }
  }
  
  return Scaffold(
    body : Row(
      children: [
        Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          decoration: BoxDecoration(color: Colors.white),
          child: StreamBuilder(
            stream: FirebaseFirestore.instance
            .collection('Users')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .collection('Coins')
            .snapshots(),
            builder: (
              BuildContext context,
              AsyncSnapshot&lt;QuerySnapshot&gt; snapshot){
                if(!snapshot.hasData){
                  return Center(child: CircularProgressIndicator(),);
                }
                return ListView(
                  children: snapshot.data!.docs.map((document) {
                    return Padding(
                      padding: const EdgeInsets.only(top: 8, left: 15, right :15),
                      child: Container(
                        width: MediaQuery.of(context).size.width/1.3,
                        height: MediaQuery.of(context).size.height/12,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15.0),
                          color: Colors.blue,
                        ),
                        child: Row(
  
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            SizedBox(width: 5,),
                            Text(&quot;Coin : ${document.id}&quot;, style: TextStyle(color: Colors.white, fontSize: 18),),
                            Text(&quot;\$${getValue(document.id, document['Amount'])}&quot;,style: TextStyle(fontSize: 18,color: Colors.white)),
                            
                            IconButton(
                              onPressed: ()async{
                                await removeCoin(document.id);
                              }, 
                              icon: Icon(Icons.close, color: Colors.red)
                              )
                            
                          ],
                        ),
                      ),
                    );
                  }).toList(),
                );
              }
            ,
          ),
        ),
        
      ],
    ),
    
    floatingActionButton: FloatingActionButton(
      onPressed: (){
        Navigator.push(context, MaterialPageRoute(builder: (context) =&gt; AddView()));
      },
      child: Icon(Icons.add, color: Colors.white,),
      backgroundColor: Colors.blue,
      ),
  );
  }
  }
  </code></pre>
  <p>And here's the function for the api:</p>
  <pre><code>Future&lt;double&gt; getPrice(String id) async{
  try{
  var url = &quot;https://api.coingecko.com/api/v3/coins/&quot; + id;
  var response = await  http.get(Uri.parse(url));
  var json = jsonDecode(response.body);
  var value = json['market_data']['current_price']['usd'].toString();
  return double.parse(value);
  }catch(e){
  print(e.toString());
  return 0.0;
  }
  }
  </code></pre>
  <p>Any idea would be super helpul!
  Thank you guys!</p>