gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:13:06 28 4
gpt4 key购买 nike

我正在尝试按如下方式过滤 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?

谢谢!

最佳答案

我不太确定你问的是什么。大概 List.where()方法?

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

关于带有 if 语句的 Flutter Firestore FutureBuilder 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53451737/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com