【问题标题】:Delete firestore document using Slidable使用 Slidable 删除 Firestore 文档
【发布时间】:2022-01-16 00:59:53
【问题描述】:

我正在尝试实现一个可滑动的小部件以从 Firestore 快照中删除选定的文档。这是我的代码:

return Expanded(
    child: ListView(
      children: snapshot.data!.docs.map((DocumentSnapshot document) {
        Map<String, dynamic> data =
            document.data()! as Map<String, dynamic>;
        return Card(
          color: const Color(0xff303437),
          elevation: 0,
          margin: const EdgeInsets.only(
            top: 10,
            bottom: 10,
            right: 0,
            left: 0,
          ),
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(20),
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: Slidable(
              endActionPane: ActionPane(
                motion: const ScrollMotion(),
                children: [
                  SlidableAction(
                    key: ObjectKey(data['date']),
                    onPressed: deleteData,
                    backgroundColor: const Color(0xfffe4A49),
                    foregroundColor: Colors.white,
                    icon: Icons.delete,
                    label: 'Delete',
                  ),
                ],
              ),
              child: ListTile(
                ..
              )

现在函数 deleteData 只做这个:

void deleteData(BuildContext context) {
  debugPrint(context.widget.key.toString());
}

我想要完成的是:使用文档“日期”作为删除它的键,但 debugPrint 每次都给我 null。我确信 data['date'] 有效,因为我可以在 ListTile 中看到它们中的每一个。

如何将密钥传递给 deleteData 函数,以便按时间戳过滤所有文档并删除正确的文档?

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    我使用dismissible解决了:

    child: Slidable(
      key: ValueKey(data['date']),
      endActionPane: ActionPane(
        extentRatio: 0.3,
        motion: const ScrollMotion(),
        dismissible: DismissiblePane(
          onDismissed: () {
            debugPrint(data['date'].toString());
            FirebaseFirestore.instance
              .collection('users/$uid/receipts')
              .where('date', isEqualTo: data['date'])
              .limit(1)
              .get()
              .then((value) {
                value.docs.forEach((element) {
                  FirebaseFirestore.instance
                    .collection('users/$uid/receipts')
                    .doc(element.id)
                    .delete();
                  });
                });
              },
            ),
            children: const [
              SlidableAction(
                onPressed: doNothing,
                backgroundColor: Color(0xfffe4a49),
                foregroundColor: Colors.white,
                icon: Icons.delete,
                label: 'Delete',
              ),
            ],
          ),
          child: ListTile(
            ..
          ),
        ),
    

    现在 onPressed 动作什么也不做。 当我找到它时,我会提供一个完整的实现,现在这已经足够了。

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2020-09-01
      • 1970-01-01
      • 2021-03-16
      • 2021-10-16
      • 2020-10-28
      相关资源
      最近更新 更多