简体   繁体   English

如何使用特定参数 flutter 从 Firestore 中删除数据?

[英]How to delete data from Firestore with specific parameter flutter?

I'm trying to delete some data with where parameter to specify which one should be deleted.我正在尝试使用where参数删除一些数据以指定应该删除哪个数据。

I've this method and it works perfectly.我有这个方法,而且效果很好。

final FirebaseFirestore _db = FirebaseFirestore.instance;

Future deleteInstructorDocumentInFirestore(String url) async {
    _db.collection(instructorsDocument)
        .where("photo", isEqualTo: url)
        .get()
        .then((value) {
          for (var element in value.docs) {
            _db.collection(instructorsDocument)
                .doc(element.id)
                .delete();
          }
    });
  }

So I've tried the same thing with this one but it doesn't work.所以我用这个尝试了同样的事情,但它不起作用。

Firestore火库

在此处输入图像描述

Function Function

final FirebaseFirestore _db = FirebaseFirestore.instance;

Future deleteEvent(String uid, DateTime from, DateTime to) async {
    print("From: $from");
    print("From type: ${from.runtimeType}");
    print("To: $to");
    print("To type: ${to.runtimeType}");
    _db.collection(instructorsEvent)
        .where("uid", isEqualTo: uid)
        .where("from", isEqualTo: from)
        .where("to", isEqualTo: to)
        .get()
        .then((value) {
          for (var element in value.docs) {
            _db.collection(instructorsEvent)
                .doc(element.id)
                .delete();
          }
        });
  }

在此处输入图像描述

What am I doing wrong.我究竟做错了什么。

Thanks in advance提前致谢

The from and to fields are strings. fromto字段是字符串。 But you are trying to compare them to dart DateTime object, which are not strings.但是您试图将它们与不是字符串的 dart DateTime object 进行比较。 As such, they will not match in any query - your queries are simply returning no documents at all, so there is nothing to delete.因此,它们在任何查询中都不会匹配 - 您的查询根本不返回任何文档,因此没有什么可删除的。 (I suggest adding some debug logging to better observe this for yourself.) (我建议添加一些调试日志以更好地自己观察这一点。)

Firestore will not convert DateTime objects to strings for you, nor will it try to guess how to compare them for you. Firestore 不会为您将 DateTime 对象转换为字符串,也不会尝试猜测如何为您比较它们。 You must provide a similar type of value to the document field you are trying to compare to.您必须为您尝试比较的文档字段提供类似类型的值。

So, if your document fields must remain strings, then you should find a way to convert your DateTime objects to strings before handing them to a Firestore query.因此,如果您的文档字段必须保留字符串,那么您应该找到一种方法将 DateTime 对象转换为字符串,然后再将它们交给 Firestore 查询。 Or, you must find another way of representing points in time (in Firestore, usually a timestamp type field), then provide objects to the query that Firestore naturally understand to be comparable to timestamps.或者,您必须找到另一种表示时间点的方式(在 Firestore 中,通常是时间戳类型字段),然后将对象提供给 Firestore 自然理解为与时间戳相当的查询。

See also:也可以看看:

Problem Solved问题解决了

@Doug Stevenson thanks @Doug Stevenson 谢谢

Utils实用程序

static String toDateTimeToStringFromFirestore(DateTime dateTime) {
  final date = DateFormat("yyyy-MM-ddTHH:mm:ss.SSS").format(dateTime);

  return date;
}

EventViewModel事件视图模型

Future deleteEvent(String uid, DateTime from, DateTime to) async {
    final fromString = Utils.toDateTimeToStringFromFirestore(from);
    final toString = Utils.toDateTimeToStringFromFirestore(to);
    
    _db.collection(instructorsEvent)
        .where("uid", isEqualTo: uid)
        .where("from", isEqualTo: fromString)
        .where("to", isEqualTo: toString)
        .get()
        .then((value) {
          for (var element in value.docs) {
            _db.collection(instructorsEvent)
                .doc(element.id)
                .delete();
          }
        });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM