Karel Debedts
Karel Debedts

Reputation: 5788

Flutter + firestore : filter list

I have a function that gets documents from firestore.

  List<Post> posts;
  List<Post> filteredposts;

  void getDeals() async {
    QuerySnapshot snapshot = await Firestore.instance
        .collection('deals')
        .document('${widget.stad.toLowerCase()}')
        .collection('deals')
        .orderBy('endTime', descending: false)
        .getDocuments();
    List<Post> posts =
        snapshot.documents.map((doc) => Post.fromDocument(doc)).toList();
    setState(() {
      this.posts = posts;
    });
  }

I would like to filter that list e.x. where the bool: (in the firestore document) "typeX" is equalto true.

  setState(() {
      filteredposts = posts.where((snapshot) => snapshot.typeX == true);
    });

But I get the error message:

'WhereIterable<Post>' is not a subtype of type 'List<Post>'

What am I doing wrong?

Thank you very much!

Upvotes: 1

Views: 2919

Answers (1)

Chenna Reddy
Chenna Reddy

Reputation: 2341

Many of the List/Iterable helper methods like map, where, expand... etc return a variant of Iterable but not List as the underlying data structure need not be a List (e.g. it can be a Set).

So most of the times, you need to call toSet or toList on the result of these methods.

In your case it would be

filteredposts = posts.where((snapshot) => snapshot.typeX == true).toList();

Upvotes: 1

Related Questions