gpt4 book ai didi

android - 无法检查从Cloud Firestore数据库检索的文档是否具有数据

转载 作者:行者123 更新时间:2023-12-03 03:49:57 26 4
gpt4 key购买 nike

我正在从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

关于android - 无法检查从Cloud Firestore数据库检索的文档是否具有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63859582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com