我正在构建一个带有可关闭小部件、firebase 和 StreamBuilder 的 flutter 应用程序,并收到以下错误“已关闭的可关闭小部件仍然是树的一部分”。
请找到以下代码。
Expanded(
child: StreamBuilder(
stream: Firestore.instance
.document('/users/User1/Trips/${widget.tripId}')
.collection('TropDocs')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text("Loading....");
return ListView.builder(
itemExtent: 150.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
final item = snapshot.data.documents[index];
final itemID =
snapshot.data.documents[index].documentID;
final list = snapshot.data.documents;
return Dismissible(
key: Key(itemID),
// We also need to provide a function that tells our app
// what to do after an item has been swiped away.
onDismissed: (direction) {
// Remove the item from our data source.
//fBtDoc.deleteTraveldoc(item);
//Firestore.instance.collection('/users/User1/Trips/${widget.tripId}/TropDocs/').document('$itemID').delete();
setState(() {
list.removeAt(index);
});
// Then show a snackbar!
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text("$item dismissed")));
},
// Show a red background as the item is swiped away
background: Container(color: Colors.red),
child: _buildlistitem(
context, snapshot.data.documents[index])
);
}
);
},
),
)
Run Code Online (Sandbox Code Playgroud)
小智 16
在我的事业中,我使用了如下
key: Key(index),
Run Code Online (Sandbox Code Playgroud)
然后我确实将其更改如下。它正在工作
key: UniqueKey(),
Run Code Online (Sandbox Code Playgroud)
小智 14
也许,我迟到了,但我认为这对其他人有帮助。有同样的问题,甚至设置
key: Key(itemId[index]),
Run Code Online (Sandbox Code Playgroud)
没有用。然而,
key: UniqueKey(),
Run Code Online (Sandbox Code Playgroud)
非常适合我
小智 13
原因是传递给 key 属性的值\xe2\x80\x99 不是唯一的或与列表的索引相关。\n解决方案非常简单,您所需要做的就是将 key 属性设置为 UniqueKey(),像这样:
\nkey: UniqueKey(),\n
Run Code Online (Sandbox Code Playgroud)\n
移除 setState 块,streamBuilder 将自行重建列表
setState(() {
list.removeAt(index);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7544 次 |
最近记录: |