Florian K
Florian K

Reputation: 2148

Cloud firestore query of Map returning empty snapshot in Flutter/Dart

I do a query to get a document by checking a Map key from firestore. I use a StreamBuilder for that. Both Id are the same in firestore and in flutter code. Do you have an idea why the snapshot is empty ? postSnapshot.data.documents.length is equal to 0, it returns [].

StreamBuilder(
        stream: Firestore.instance
            .collection('posts')
            .orderBy('createdAt', descending: true)
            .where('userlist.' + widget.user.uid, isEqualTo: true)
            .snapshots(),
        builder: (context, postSnapshot) {
          if (postSnapshot.connectionState == ConnectionState.waiting) {
            return _displaysLoading();
          }
          if (postSnapshot.hasData) {
           ...
          }
        }
    )

enter image description here

Upvotes: 0

Views: 525

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317372

Your query is returning no documents. That's because your data doesn't have a boolean true as the value of the map. It has "user1". A query using isEqualTo: true will never match true to the string value you show here.

You will either need to make your query match the data and pass "user1", or make your data match the query by making the value a boolean true.

Upvotes: 1

Niteesh
Niteesh

Reputation: 3130

try this

StreamBuilder(
        stream: Firestore.instance
            .collection('posts')
            .orderBy('createdAt', descending: true)
            .where('userlist', arrayContains: widget.user.uid)
            .snapshots(),
        builder: (context, postSnapshot) {
          if (postSnapshot.connectionState == ConnectionState.waiting) {
            return _displaysLoading();
          }
          if (postSnapshot.hasData) {
           ...
          }
        }
    )

Upvotes: 0

Related Questions