Home > Net >  How to delete data from Firestore with specific parameter flutter?
How to delete data from Firestore with specific parameter flutter?

Time:08-28

I'm trying to delete some data with where parameter to specify which one should be deleted.

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

enter image description here

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();
          }
        });
  }

enter image description here

What am I doing wrong.

Thanks in advance

CodePudding user response:

The from and to fields are strings. But you are trying to compare them to dart DateTime object, which are not strings. 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. 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. 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.

See also:

CodePudding user response:

Problem Solved

@Doug Stevenson thanks

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();
          }
        });
  }
  • Related