【问题标题】:StreamBuilder returns nothing in the snapshotStreamBuilder 在快照中不返回任何内容
【发布时间】:2020-10-16 22:42:37
【问题描述】:

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

 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

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

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

    您可以查看有关 ConnectionState 枚举的文档,了解更多信息,该枚举表示 AsyncSnapshot 的状态。

    也就是说,使用builderStreamBuilder 可以做的最基本的事情是:

    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());
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2019-04-06
      相关资源
      最近更新 更多