Home > Enterprise >  How to filter firebase data using a where clause with FieldPat.documentId
How to filter firebase data using a where clause with FieldPat.documentId

Time:09-10

Im trying to retrieve data with

Stream<List<User>> getUsers(User user) {
    List<String> userFilter = List.from(user.swipeLeft!)
      ..addAll(user.swipeRight!)
      ..add(user.uid!);

    return _firebaseFirestore
        .collection('users')
        .where('interestedIn', isEqualTo: 'HIRING')
        .where(FieldPath.documentId, whereNotIn: userFilter)
        .snapshots()
        .map((snap) {
      return snap.docs.map((doc) => User.fromSnapshot(doc)).toList();
    });
  }

I get an error An error occurred while parsing query arguments, this is most likely an error with this SDK. Invalid query. When querying with FieldPath.documentId() you must provide a valid document ID, but it was an empty string.

My data in firebase is structured as follows enter image description here

How can I fix this?

CodePudding user response:

In firebase you cannot filter with the document id, you can only filter with the fields in the document, a way a simple solution will be to generate a unique id by your self using packages like uuid and then save the document with the id and also save the id inside the document fields then you can filter with the id in the document field

  • Related