StreamBuilder在快照中不返回任何内容

时间:2020-06-26 10:34:05

标签: firebase flutter dart google-cloud-firestore

当我在有状态的小部件内使用streamBuilder时,我在firebase上手动创建的带有一些示例字段的数据库中设置了一个流,快照.data不返回任何内容/为空。

 final CollectionReference **storeCollection** =  Firestore.instance.collection('stores'); 
                  

Stream**<List<Stores>>** get **stores** {
         return **storeCollection.snapshots()**.map(_storeListFromSnapshot);

然后在我使用StreamBuilder获取snapshot.data但它返回null

之后
@override
  Widget build(BuildContext context) {
    return **StreamBuilder**<Object>(
        stream: DatabaseService().**stores**,
        builder: (context, **snapshot**) {
          **List<Stores> stores** = **snapshot.data** ?? []; //returns null on this line

我能够使用storeCollection.document(uid).setData()将数据更新到Firebase

1 个答案:

答案 0 :(得分:0)

通常在侦听开始时会收到AsyncSnapshot,以表明订阅处于活动状态,它正在等待某些内容,并且其中不包含数据。

您可以找到更多有关ConnectionState枚举的文档的信息,这些枚举表示AsyncSnapshot的状态。

也就是说,您可以使用builder中的StreamBuilder做的最基本的事情是:

builder: (context, snapshot){
  if (snapshot.hasData){
    List<Stores> stores = snapshot.data;
    //the rest of your logic on stores
  }else{ //nothing yet, we're waiting for the event, show a loader or whatever
   return Center(child: CircularProgressIndicator());
  }
}