簡體   English   中英

無法檢查從 Cloud Firestore 數據庫檢索的文檔是否有數據

[英]failing to check if document retrieved from cloud firestore database has data or not

我正在從 Firestore 檢索數據,但我的代碼似乎無法正常工作。 我希望它在我給它的集合中找不到數據時顯示一些東西...................................... ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ……………………

這是我的代碼:

     StreamBuilder( 
                      stream: Firestore.instance.collection("payments").where('participants', arrayContains: userActive).snapshots(),
                      builder: (context, snapshot){


                        
                     

                      return Container ( child:ListView.builder(
                          shrinkWrap: true,
                        itemCount: snapshot.data.documents.length,
                        padding: EdgeInsets.all(0),
                        controller: ScrollController(keepScrollOffset: false),
                        itemBuilder: (context, index){

                           DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data();

                          if(snapshot.data.documents.isEmpty){

                               print("No Data!!!");


                          }else{


                            print("Found Data!!!");
                          }

                     /*   if(documentSnapshot["receiver_name"] == userActive )  {         
                                                            

                                                              
                            return Container(
                            margin: EdgeInsets.symmetric(horizontal: 32,vertical: 5),
                            padding: EdgeInsets.all(16),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.all(Radius.circular(20))
                            ),
                            child: Row(
                              children: <Widget>[
                                Container(
                                  decoration: BoxDecoration(
                                      color: Colors.grey[100],
                                      borderRadius: BorderRadius.all(Radius.circular(18))
                                  ),
                                  child: Icon(Icons.attach_money, color: Colors.lightBlue[900],),
                                  padding: EdgeInsets.all(12),
                                ),

                                SizedBox(width: 16,),
                                Expanded(
                                  
                                  child: Column(


                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: <Widget>[
                                      
                                    Text("Recieved", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: Colors.grey[900]),) ,

                                    
                                   
                                      Text("" + documentSnapshot["currency_received"] + documentSnapshot["amount_received"], style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: Colors.grey[500]),),
                                   

                                   
                                     
                                                                      
                                   
                                    ],
                                  ),
                                ),

                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: <Widget>[

                                    


                                    Text("+ " + documentSnapshot["currency_sent"] + documentSnapshot["amount_paid"].toString(), style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: Colors.lightGreen),),



                                
 Text(documentSnapshot["date"].toDate().toString(), style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: Colors.grey[500]),),                                
                                  ],
                                ),
                              ],
                            ),
                          );


                      } else if (documentSnapshot["sender_name"] == userActive){



                     return Container(
                            margin: EdgeInsets.symmetric(horizontal: 32,vertical: 5),
                            padding: EdgeInsets.all(16),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.all(Radius.circular(20))
                            ),
                            child: Row(
                              children: <Widget>[
                                Container(
                                  decoration: BoxDecoration(
                                      color: Colors.grey[100],
                                      borderRadius: BorderRadius.all(Radius.circular(18))
                                  ),
                                  child: Icon(Icons.attach_money, color: Colors.lightBlue[900],),
                                  padding: EdgeInsets.all(12),
                                ),

                                SizedBox(width: 16,),
                                Expanded(
                                  
                                  child: Column(


                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: <Widget>[
                                      
                                    Text("Sent", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: Colors.grey[900]),) ,

                                    
                                   
                                      Text("" +  documentSnapshot["currency_received"] + documentSnapshot["amount_received"], style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: Colors.grey[500]),),
                                   

                                   
                                     
                                                                      
                                   
                                    ],
                                  ),
                                ),

                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: <Widget>[

                                    


                                    Text("- " + documentSnapshot["currency_sent"] + documentSnapshot["amount_paid"].toString(), style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: Colors.orange),),



                                
                                                 Text(documentSnapshot["date"].toDate().toString(), style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: Colors.grey[500]),),
                                
                                  ],
                                ),
                              ],
                            ),
                          );








                        
                 



                      }else{
                                    
                                        
                                        return Text("Nothng found ",style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: Colors.black));
                      }


*/
                                                            
                       
                        

                        },
                       )
                      );
                        
 }
 



  
   ),
                 

最近 Firestore 有一個新更新,您必須使用.data()檢索該值。

為了讓你的代碼工作,你必須改變這個:

DocumentSnapshot documentSnapshot = snapshot.data.docs[index]; //not working code

最后添加.data()

DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data(); //working code

檢查您的cloud_firestore版本,如果您使用的是cloud_firestore: ^0.14.0版本cloud_firestore: ^0.14.0

DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data();

如果不使用

DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data; //this is a getter

data 是一個 getter,而不是一個方法,應該在沒有括號的情況下調用(javascript 使用它們是因為它是一個方法,但不是在 dart 中),但只能在 0.14.0 版本之前調用。 如果您不確定可以運行pub outdated來檢查軟件包的當前版本。

也最好像這樣改變它

StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance.collection("payments").where('participants', arrayContains: 'userActive').snapshots(),
  builder: (context, snapshot){
    if(snapshot.connectionState == ConnectionState.active && snapshot.hasData) {
      if(snapshot.data.documents.isEmpty) return Center(child: const Text('No Data!'));
      print("Found Data!!!");
      return Container ( child:ListView.builder(...)); //your listView here
    }
    return Center(child: const CircularProgressIndicator());
  },
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM