【发布时间】:2021-04-29 17:54:26
【问题描述】:
我正在尝试从 Cloud Firestore 制作一个简单的流。我的 Firestore 数据中有内容,但 Stream 似乎不起作用。
这就是我构建 Stream 的方式:
body: StreamBuilder(
stream: Firestore.instance.collection('user_data').document('rCqMQ3oLjBLk6yg6P1oT').collection('Buttons').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError){
return Container(color: Colors.red);
}
if (!snapshot.hasData){
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasData){
return GridView.count(
padding: EdgeInsets.all(15),
crossAxisSpacing: 20.0,
mainAxisSpacing: 20.0,
crossAxisCount: 3,
children: [
CreateCard("ola"),
GestureDetector(
onTap: () async{
print(snapshot.data);
},
child: Container(
color: Colors.black,
width: 150,
height: 150,
child: Icon(Icons.add, color: Colors.white,),
),
),
],
);
}
}
【问题讨论】:
-
你的代码看起来不错;在流进入
active状态后,snapshot.data应该有DocumentSnapshot的列表。您是否尝试过检查connectionstate而不是hasData布尔值,例如 - api.flutter.dev/flutter/widgets/StreamBuilder-class.html 中给出的示例代码 -
我已经创建了连接状态,但是它进入了等待状态。我打印了 snapshot.data 并收到了 null
-
如果卡在等待状态,可能是网络/连接问题..您只能在
active or done状态下获得数据snapshot.data.. -
但是我怎么会遇到连接问题?
-
可能是个例.. 设备网络稳定吗?
标签: flutter dart google-cloud-firestore