带有if语句的Flutter Firestore FutureBuilder过滤器

时间:2018-11-23 18:55:45

标签: flutter google-cloud-firestore

我正在尝试按以下方式过滤FutureBuilder的列表:

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();
                      }
                    });

当我删除if语句时,此FutureBuilder起作用;但是,当我包含以下if语句时,Flutter会引发错误:

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(),
 ),

我的问题:当文档快照字段具有特定值(在这种情况下,名字为“ James”时)如何仅返回FindFollowerWidget?

谢谢!

1 个答案:

答案 0 :(得分:2)

我不确定您要问什么。可能是List.where()方法?

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