StreamBuilder 刷新时 AnimatedList 超出范围
AnimatedList out of range when StreamBuilder refreshes
StreamBuilder(
stream: users.where("invites", arrayContains: uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
_items = snapshot.data!.docs;
return AnimatedList(
key: listKey,
initialItemCount: _items.length,
itemBuilder: (context, index, animation) {
return _buildItem(index, _items[index], animation);
},
);
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Center(
child: SizedBox(
height: 50,
width: 50,
child: CupertinoActivityIndicator()),
),
],
);
}
},
)
我通过来自 FirebaseFirestore 的流获得了这个 StreamBuilder。例如。我在 Firebase 中有 3 个条目符合我的条件(保存在 _items 中)。到目前为止,一切都很好。但是,如果我删除其中一个,流会刷新,但列表会抛出错误:RangeError (index): Invalid value: Not in inclusive range 0..1: 2
反过来解决同样的问题:只显示了两个,没有显示第三个。
有谁知道为什么或如何解决这个问题。
谢谢:)
这不适用于动画列表,因为您的索引正在减少。您需要构建一个新的动画列表,否则我建议您使用普通列表视图。
StreamBuilder(
stream: users.where("invites", arrayContains: uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
_items = snapshot.data!.docs;
return AnimatedList(
key: listKey,
initialItemCount: _items.length,
itemBuilder: (context, index, animation) {
return _buildItem(index, _items[index], animation);
},
);
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Center(
child: SizedBox(
height: 50,
width: 50,
child: CupertinoActivityIndicator()),
),
],
);
}
},
)
我通过来自 FirebaseFirestore 的流获得了这个 StreamBuilder。例如。我在 Firebase 中有 3 个条目符合我的条件(保存在 _items 中)。到目前为止,一切都很好。但是,如果我删除其中一个,流会刷新,但列表会抛出错误:RangeError (index): Invalid value: Not in inclusive range 0..1: 2 反过来解决同样的问题:只显示了两个,没有显示第三个。 有谁知道为什么或如何解决这个问题。 谢谢:)
这不适用于动画列表,因为您的索引正在减少。您需要构建一个新的动画列表,否则我建议您使用普通列表视图。