flutter - 如何使用Dissmissable从itembuilder中删除项目,并且该项目来自Firestore?

标签 flutter listview dart google-cloud-firestore dismiss

  @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中删除该项目。
  • 我考虑过将每个项目放入 map 中,然后根据 map 构建卡片。我宁愿不那样做。

  • 感谢您的帮助,希望我们可以一起解决这个问题:)
    先感谢您!

    最佳答案

    首先通过以下操作获取文档引用:
    对此进行迭代:snapshot.data.documents然后snapshot.data.documents[i].documentID然后这个:

    onDismissed: (direction) async {
        await Firestore.instance.runTransaction(
          (transaction) async {
            await transaction.delete(your doc ref here);
          },
        );
      },
    

    关于flutter - 如何使用Dissmissable从itembuilder中删除项目,并且该项目来自Firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64021959/

    相关文章:

    firebase - 如何从 DropdownButtonFormField 获取两个值

    c# - 为什么 ListView 在 MVVM 中不更新

    flutter - 如何格式化表单数据标题的主体

    web - Flutter Web - 如何重新加载当前事件页面

    Flutter - Marquee 没有为类 'MyApp' 定义

    android - 是什么导致 "permission denied"- 带有 FIREBASE 和 FLUTTER 的消息

    c# - 在 ListView 列标题中的列区域之外绘制

    android - 自定义 listView 按随机顺序显示较少的项目

    dart - 错误 : Only static members can be accessed in initializers what does this mean?

    flutter - 在没有streambuilder的情况下获取firebase数据