【问题标题】:Is there away to send a list of object generated by a stream builder to a local method?是否可以将流生成器生成的对象列表发送到本地方法?
【发布时间】:2020-07-30 07:43:48
【问题描述】:

所以我正在尝试将流生成器生成的对象列表发送到这个名为“submitOrder”的方法,但这似乎是一种错误的方法,如果有人可以帮助我,我将不胜感激。

StreamBuilder(
                    stream: DatabaseService().cartCollection.snapshots(),
                    builder: ( context,  snapshot){
                      if (!snapshot.hasData){  
                       return Loading();
                                          }
                              else{  

                        List<CartItem> products =[];
                        double total =0;
                        CartItem temp= CartItem(customerId:'' ,itemId:'' ,id: '', title: '', quantity:0, price: 0);

                        for (int i=0; i<snapshot.data.documents.length; i++){

                          DocumentSnapshot snap = snapshot.data.documents[i];

                         if ((snap.data['customerId']== user.uid && snap.data['status']== 'open') ){

                           temp = CartItem(
                                                customerId: user.uid, 
                                                itemId: snap.data['itemId'].toString(),
                                                id: snap.documentID.toString(), 
                                                title: snap.data['itemName'].toString(), 
                                                quantity: int.tryParse(snap.data['quantity'].toString()) , 
                                                price: double.tryParse( snap.data['price'].toString())
                                                );

                                                total += temp.quantity*temp.price;
                                                products.add(temp);

                                }    



                        } 

                        submitOrder(products,total); 
                        return Text('Order Sucsessfuly!');
                      }
                   }
            );


【问题讨论】:

  • 问题似乎是你没有得到你的数据

标签: firebase flutter dart stream-builder


【解决方案1】:

试试这个,让我知道它是否适合你

StreamBuilder<QuerySnapshot>(
                    stream: DatabaseService().cartCollection.snapshots(),
                    builder: (BuildContext context,AsyncSnapshot<QuerySnapshot>  snapshot){
                      if (snapshot.hasData){  
                        List<CartItem> products =[];
                        double total =0;
                        CartItem  temp;


                       snapshot.data.documents.map((DocumentChange change){
                       if ((change.document.data['customerId']== user.uid && change.document.data['status']== 'open') ){

                          temp = CartItem(
                                                customerId: user.uid, 
                                                itemId: snap.data['itemId'].toString(),
                                                id: snap.documentID.toString(), 
                                                title: snap.data['itemName'].toString(), 
                                                quantity: int.tryParse(snap.data['quantity'].toString()) , 
                                                price: double.tryParse( snap.data['price'].toString())
                                                );

                                                total += temp.quantity*temp.price;
                                                products.add(temp);
                       }
                                     });


                        submitOrder(products,total); 
                        return Text('Order Sucsessfuly!');

                     } else{  
                        return Loading();

                      }
                   }
            );

【讨论】:

    猜你喜欢
    • 2021-04-18
    • 2016-04-01
    • 2016-03-16
    • 1970-01-01
    • 2015-10-14
    • 2012-09-20
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多