我正在使用可弃用的小部件StreamBuilder构建一个Flutter应用程序,并且正在从Firebase中获取数据 但是onDismissed我遇到了错误: 已关闭的“可关闭”小部件仍是树的一部分。
如果有人知道如何在StreamBuilder中使用可禁用的小部件,请共享一些示例代码。
或者让我知道我在哪里做错了。
这是我的main.dart代码
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])
);
}
);
},
),
)