flutter - 添加 firestore 文档时更新 ListView.builder itemCount

标签 flutter dart google-cloud-firestore

我有一个 flutter 应用程序,其中使用 ListView.Builder 生成列表,其中 itemCount 是 firestore 集合中的文档数。在添加新文档之前,这可以正常工作。发生这种情况时,我会收到错误消息(17 和 18 只是示例)。

Invalid value: Not in range 0..17, inclusive: 18

我假设我需要在创建新文档时更新状态,但我不知道发生这种情况时如何调用 setState

这是代码的相关部分:

child: StreamBuilder(
                stream: Firestore.instance.collection('contact').orderBy(sortby, descending: decending).snapshots(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData) return Container();
                  return ListView.builder(
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index) =>
                        _personer(context, snapshot.data.documents[index], index),
                  );
                },
              ),

最佳答案

使用设置状态?

 StreamBuilder(builder: (context, snapshot) {
   return snapshot.hasData == null ? Container() : _getListView(snapshot);
 } , )

_getListView(snapshot) {
setState(() {
  return ListView.builder(
    itemCount: snapshot.data.documents.length,
    itemBuilder: (context, index) =>
        _personer(context, snapshot.data.documents[index], index),
  );
});

关于flutter - 添加 firestore 文档时更新 ListView.builder itemCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57621540/

相关文章:

node.js - 如何在 Firestore 中进行时间戳查询

flutter - 我怎样才能在 flutter 中删除这个底部导航栏上的白色背景

flutter - 如何调整TextField的宽度?

flutter - Firestore错误 'controller != null':不正确

dart - 以编程方式使 dart 中的十六进制颜色变亮或变暗

google-cloud-firestore - 对于 Cloud Bigtable,我们是否需要至少 3 个节点一直运行?

android - Flutter : Playback error E/ExoPlayerImplInternal( 8813): com. google.android.exoplayer2.ExoPlaybackException:源错误

flutter - 关闭小部件时如何将 : Remove separator from ListView. 分开?

flutter - 如何将文本添加到 TextField

ios - 使用 Firestore 中的数据填充 TableView