Google cloud firestore Flatter webapp:Snapshort返回空值
我是一个新手,一直在尝试从firebase检索数据并显示,但其快照为空。我也遇到过其他用户提出的类似问题,但没有任何效果Google cloud firestore Flatter webapp:Snapshort返回空值,google-cloud-firestore,snapshot,flutter-web,stream-builder,Google Cloud Firestore,Snapshot,Flutter Web,Stream Builder,我是一个新手,一直在尝试从firebase检索数据并显示,但其快照为空。我也遇到过其他用户提出的类似问题,但没有任何效果 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: BackButton( color: Colors.white, ), bottomOpacity: 1.0, backgroundColor: mucolor1,
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: BackButton(
color: Colors.white,
),
bottomOpacity: 1.0,
backgroundColor: mucolor1,
title: Text(
"Customer receipt ",
style: TextStyle(color: Colors.white, fontSize: 12),
),
),
body: Stack(
clipBehavior: Clip.none,
alignment: Alignment.topCenter,
children: [
Container(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection("users")
.doc(tableNo)
.collection("order")
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting)
return new Center(child: new CircularProgressIndicator());
if(snapshot.data!=null){
return Container(
margin: EdgeInsets.fromLTRB(20, 100, 20, 0),
child: ListView(
children: snapshot.data.docs.map((document) {
return Container(
color: Colors.white54,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
elevation: 0,
child: Container(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
Text("Tsh " +
document.get("price").toString() +
"/="),
Text(document.get("qty").toString()),
Text(document.get("title")),
],
),
),
)
],
));
}).toList(),
),
);
}else {
return Positioned(
top: 80,
child: Center(
child: Container(
margin: EdgeInsets.fromLTRB(20, 10, 20, 0),
child: Text("No data"))),
);
}
},
),
),
Positioned(
top: 10,
child: Center(
child: Container(
margin: EdgeInsets.fromLTRB(20, 10, 20, 0),
child: Text("Your Order"))),
),
Positioned(
top: 50,
child: Container(
margin: EdgeInsets.fromLTRB(20, 10, 20, 0),
child: Text(
"Total Cost Tsh " + grandTotal + "/=",
style:
TextStyle(color: mucolor2, fontWeight: FontWeight.bold),
))),
],
),
);
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
引导:后按钮(
颜色:颜色,白色,
),
底部不透明度:1.0,
背景颜色:mucolor1,
标题:正文(
“客户收据”,
样式:TextStyle(颜色:Colors.white,字体大小:12),
),
),
主体:堆栈(
clipBehavior:Clip.none,
对齐:alignment.topCenter,
儿童:[
容器(
孩子:StreamBuilder(
流:FirebaseFirestore.instance
.收集(“用户”)
.doc(表格编号)
.托收(“订单”)
.snapshots(),
生成器:(BuildContext上下文,
异步快照(快照){
if(snapshot.connectionState==connectionState.waiting)
返回新中心(子项:new CircularProgressIndicator());
if(snapshot.data!=null){
返回容器(
边距:LTRB(20,100,20,0)的边距集,
子:ListView(
子项:snapshot.data.docs.map((文档){
返回容器(
颜色:颜色。白色54,
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
卡片(
海拔:0,
子:容器(
填充:边缘设置。全部(10),
孩子:排(
主轴对准:
MainAxisAlignment.spaceAround,
儿童:[
文本(“Tsh”+
document.get(“price”).toString()+
"/="),
文本(document.get(“数量”).toString()),
文本(document.get(“title”),
],
),
),
)
],
));
}).toList(),
),
);
}否则{
返回定位(
排名:80,
儿童:中心(
子:容器(
边距:从LTRB(20,10,20,0)开始的边距集,
子项:文本(“无数据”),
);
}
},
),
),
定位(
前10名,
儿童:中心(
子:容器(
边距:从LTRB(20,10,20,0)开始的边距集,
孩子:文本(“您的订单”),
),
定位(
前50名,
子:容器(
边距:从LTRB(20,10,20,0)开始的边距集,
子:文本(
“总成本Tsh”+grandTotal+“/=”,
风格:
TextStyle(颜色:mucolor2,字体:fontwweight.bold),
))),
],
),
);
}
}
以下是我的数据库结构:
我需要你的帮助。我已经努力解决这个问题一个月了。您的
用户
文档是空的;这意味着在它的位置上实际上没有文档
在这种情况下,您应使用集合组(“订单”)
-
一个很好的参考:我将firebase SDK js版本8.2.1替换为7.22.1,它可以正常工作 更换前 SDK 8.2.1
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-analytics.js"></script>
SDK 7.22.1
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-analytics.js"></script>
现在可以使用了。您尝试过使用collectionGroup(“订单”)-谢谢您的建议,但我还是遇到了同样的问题。我试图检查快照连接状态,它返回notconnected
if(snapshot.connectionState!=connectionState.done){返回Positioned(top:80,child:Center(child:Container)(margin:EdgeInsets.fromLTRB(20,10,20,0),child:Text(“未连接”);}
以下是我的数据库规则……RULES_version='2';service cloud.firestore{match/databases/{database}/documents{match/{document=**}{allow write,read;}
好消息是我可以从web应用程序在数据库上写入数据。因此我想知道我缺少了什么?请帮助我检查Streambuilder
是否检查ConnectionState.active-数据是否为非空,并且可能随时间而更改。
或snapshot.hasdata-返回此快照是否包含非空数据ll数据值
。是的……感谢连接状态正常。它已连接,但仍然“无数据”。