Android 无法检查从cloud firestore数据库检索的文档是否包含数据
我正在从Firestore中检索数据,但我的代码似乎无法正常工作。我希望它在我提供给它的集合中找不到数据时显示一些内容 这是我的密码:Android 无法检查从cloud firestore数据库检索的文档是否包含数据,android,flutter,dart,Android,Flutter,Dart,我正在从Firestore中检索数据,但我的代码似乎无法正常工作。我希望它在我提供给它的集合中找不到数据时显示一些内容 这是我的密码: StreamBuilder( stream: Firestore.instance.collection("payments").where('participants', arrayContains: userActive).snapshots(),
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));
}
*/
},
)
);
}
),
StreamBuilder(
流:Firestore.instance.collection(“payments”).where('participants',arrayContains:userActive).snapshots(),
生成器:(上下文,快照){
返回容器(子项:ListView.builder)(
收缩膜:对,
itemCount:snapshot.data.documents.length,
填充:边缘集。全部(0),
控制器:ScrollController(keepScrolOffset:false),
itemBuilder:(上下文,索引){
DocumentSnapshot DocumentSnapshot=snapshot.data.documents[index].data();
if(snapshot.data.documents.isEmpty){
打印(“无数据!!!”;
}否则{
打印(“找到数据!!!”;
}
/*如果(documentSnapshot[“receiver_name”]==userActive){
返回容器(
边缘:边缘组。对称(水平:32,垂直:5),
填充:边缘设置。全部(16),
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径。全部(半径。圆形(20))
),
孩子:排(
儿童:[
容器(
装饰:盒子装饰(
颜色:颜色。灰色[100],
边界半径:边界半径。全部(半径。圆形(18))
),
子:图标(Icons.attach_money,颜色:Colors.lightBlue[900],),
填充:边缘设置。全部(12),
),
尺寸标注框(宽度:16,),
扩大(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(“已接收”,样式:TextStyle(fontSize:18,fontWeight:fontWeight.w700,颜色:Colors.grey[900]),
文本(“+documentSnapshot[“收到的货币”]+documentSnapshot[“收到的金额”],样式:TextStyle(fontSize:15,fontWeight:fontWeight.w700,颜色:Colors.grey[500]),
],
),
),
纵队(
crossAxisAlignment:crossAxisAlignment.end,
儿童:[
文本(“+”+文档快照[“已发送的货币”]+文档快照[“已支付的金额”].toString(),样式:TextStyle(fontSize:18,fontWeight:fontWeight.w700,颜色:Colors.lightGreen),),
文本(documentSnapshot[“date”].toDate().toString(),样式:TextStyle(fontSize:15,fontWeight:fontWeight.w700,颜色:Colors.grey[500]),
],
),
],
),
);
}else if(documentSnapshot[“发件人名称”]==userActive){
返回容器(
边缘:边缘组。对称(水平:32,垂直:5),
填充:边缘设置。全部(16),
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径。全部(半径。圆形(20))
),
孩子:排(
儿童:[
容器(
装饰:盒子装饰(
颜色:颜色。灰色[100],
边界半径:边界半径。全部(半径。圆形(18))
),
子:图标(Icons.attach_money,颜色:Colors.lightBlue[900],),
填充:边缘设置。全部(12),
),
尺寸标注框(宽度:16,),
扩大(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(“已发送”,样式:TextStyle(fontSize:18,fontWeight:fontWeight.w700,颜色:Colors.grey[9
DocumentSnapshot documentSnapshot = snapshot.data.docs[index]; //not working code
DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data(); //working code
DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data();
DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data; //this is a getter
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());
},
);