您如何使用 Dissmissable 从 itembuilder 中删除一个项目并且该项目来自 Firestore?
How do you remove an item from from itembuilder using Dissmissable and the item is from Firestore?
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('users')
.document(widget.uid)
.collection('lists')
.snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot data = snapshot.data.documents[index];
final list = snapshot.data.documents;
return Dismissible(
// Dismissed function
key: UniqueKey(),
onDismissed: (DismissDirection direction) {
setState(() {
// This is where I would like to remove the element
// snapshot.data.remove(index);
});
},
secondaryBackground: Container(
child: Center(
child: Text('Delete',
style: TextStyle(color: Colors.white),
textAlign: TextAlign.start),
),
color: Colors.red,
),
background: Container(),
child: Card(
child: Center(
child: new Container(
padding: new EdgeInsets.all(32.0),
child: new Column(
children: <Widget>[
new Text(data['listName']),
new Text(data['Description'])
],
),
),
),
),
direction: DismissDirection.endToStart,
);
},
);
},
),
- 整个目标是让卡片元素消失并从 Firestore 中删除该项目。
- 我考虑将每个项目放入地图中,然后根据地图构建卡片。我宁愿不那样做。
感谢任何帮助,希望我们可以一起解决这个问题:)
提前致谢!
通过执行此操作首先获取文档参考:
迭代这个:snapshot.data.documents
然后snapshot.data.documents[i].documentID
然后这个:
onDismissed: (direction) async {
await Firestore.instance.runTransaction(
(transaction) async {
await transaction.delete(your doc ref here);
},
);
},
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('users')
.document(widget.uid)
.collection('lists')
.snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot data = snapshot.data.documents[index];
final list = snapshot.data.documents;
return Dismissible(
// Dismissed function
key: UniqueKey(),
onDismissed: (DismissDirection direction) {
setState(() {
// This is where I would like to remove the element
// snapshot.data.remove(index);
});
},
secondaryBackground: Container(
child: Center(
child: Text('Delete',
style: TextStyle(color: Colors.white),
textAlign: TextAlign.start),
),
color: Colors.red,
),
background: Container(),
child: Card(
child: Center(
child: new Container(
padding: new EdgeInsets.all(32.0),
child: new Column(
children: <Widget>[
new Text(data['listName']),
new Text(data['Description'])
],
),
),
),
),
direction: DismissDirection.endToStart,
);
},
);
},
),
- 整个目标是让卡片元素消失并从 Firestore 中删除该项目。
- 我考虑将每个项目放入地图中,然后根据地图构建卡片。我宁愿不那样做。
感谢任何帮助,希望我们可以一起解决这个问题:)
提前致谢!
通过执行此操作首先获取文档参考:
迭代这个:snapshot.data.documents
然后snapshot.data.documents[i].documentID
然后这个:
onDismissed: (direction) async {
await Firestore.instance.runTransaction(
(transaction) async {
await transaction.delete(your doc ref here);
},
);
},