Trying to filter down chat messages using Flutter and Firestore

Viewed 255

I'm trying to create a Blocked Users list in my chat app using the latest versions of Flutter Beta channel (1.23.0-18.1.pre) and cloud_firestore 0.14.3.

Here's my data structure:

Firestore

At first, I tried something like this (Hardcoded just to test), by filtering the messages I'm querying from Firestore. Firebase doesn't like this.

query: firestore.collection('messages')
       .where('userId', whereNotIn: ['123456789', '987654321'] )
       .where('hashtag', isEqualTo: hashTag)
       .orderBy('submittedAt', descending: true),
reverse: true,

I get this error:

E/FLTFirestoreMsgCodec(24331): java.lang.IllegalArgumentException: Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'userId' and so you must also have 'userId' as your first orderBy() field, but your first orderBy() is currently on field 'submittedAt' instead.

After doing some more reading, filtering on the client-side by just hiding the messages actually better suits my needs.

Unfortunately, I'm running in circles. I'm currently thinking I would map a stream to a list, and then do something like this:

if (message.userId is in the list) {
      isBlocked = true;
    } else {
      isBlocked = false;
    } 

And then filtering out the messages if isBlocked is true. I tried hardcoding the values for that and it worked. BTW, Sorry for the pseudocode, but I deviated so many times that now I'm simply lost.

I was wondering if this was the correct approach? Any suggestions would be rad. I also tried using a future list from a stream but I couldn't get that to work either.

Future<Stream<List<BlockedUser>>> getBlockedIds() async {
            Stream<List<BlockedUser>> list;
            Stream<QuerySnapshot> snapshot = FirebaseFirestore.instance.collection('user').doc('id').collection('blocked').snapshots();
            list = snapshot.map((query) => query.docs.map(
                    (doc) => BlockedUser(
                    id: doc.data()['id'])
            ).toList());
            return list;
    }

I can't get that to work since I don't know what to do with that list.

Thanks, everyone!

0 Answers
Related