我有一个 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/