PJQuakJag
PJQuakJag

Reputation: 1247

Flutter Firestore FutureBuilder filter with if statement

I'm trying to filter a list of a FutureBuilder as follows:

return new FutureBuilder(
                    future: Firestore.instance
                        .collection('user')
                        .getDocuments(),
                    builder: (BuildContext context, AsyncSnapshot snapshot2) {
                      if (snapshot2.hasData) {
                        if (snapshot2.data != null) {
                          return new Column(
                            children: <Widget>[
                              new Expanded(
                                child: new ListView(
                                  children: snapshot2.data.documents
                                      .map<Widget>((DocumentSnapshot document) {
                                        if(document['first_name'] == 'James') {
                                          return new FindFollowerWidget(
                                            name: document['first_name'] + " " + document['last_name'],
                                            username: document['username'],
                                          );
                                        }
                                  }).toList(),
                                ),
                              ),
                            ],
                          );
                        }
                      }else {
                        return new CircularProgressIndicator();
                      }
                    });

This FutureBuilder works when I remove the if statement; however, when I include the following if statement it Flutter throws an error:

new ListView(
    children: snapshot2.data.documents.map<Widget>((DocumentSnapshot document) {
        if(document['first_name'] == 'James') {
            return new FindFollowerWidget(
                name: document['first_name'] + " " + document['last_name'],
                username: document['username'],
                );
        }
    ).toList(),
 ),

My question: how can I only return the FindFollowerWidget when a document snapshot field has a specific value (in this case the first name being "James"?

Thanks!

Upvotes: 0

Views: 1459

Answers (1)

First_Strike
First_Strike

Reputation: 1089

I'm not so sure about what you are asking. Probably List.where() method?

snapshot2.data.document
    .where((document)=>document["first_name"]=="James")
    .map((document)=>FindFollowerWidget(...))
    .toList();

Upvotes: 2

Related Questions