【发布时间】:2020-01-05 22:53:06
【问题描述】:
我正在尝试获取 Firebase 集合中所有文档名称的列表以显示在 DropdownButton 中,为此我创建了一个自定义小部件 _DropdownListWithLabel。我的集合中有两个文档(我可以在 Firebase 控制台中看到这两个文档),但是当我尝试将所有文档的名称作为字符串列表检索时,只显示一个。这是执行此操作的代码的 sn-p:
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('collectionName')
.snapshots(),
builder: (context, snapshot) {
print(
"Number of documents: ${snapshot.data.documents.length},
${snapshot.data.documents.first.documentID}");
return _DropdownWithLabel(
'Label',
snapshot.data.documents.map((doc) => doc.documentID).toList());
}) //StreamBuilder
作为参考,print 语句显示如下:
Number of documents: 1, documentName
为什么这段代码不能从这个集合中检索两个文档名称?
编辑:这是 _DropdownWithLabel 小部件。如果我尝试在 ListView 或包含 Text 小部件列表的列中列出文档名称,我会得到相同的结果,即只显示一个文档名称。
class _DropdownWithLabel extends StatefulWidget {
final String label;
final List<String> dropdownItems;
_DropdownWithLabel(this.label, this.dropdownItems);
@override
State<StatefulWidget> createState() =>
_DropdownWithLabelState(label, dropdownItems);
}
class _DropdownWithLabelState extends State {
final String label;
final List<String> dropdownItems;
String selectedItem;
_DropdownWithLabelState(this.label, this.dropdownItems) {
selectedItem = dropdownItems.first;
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(label),
Container(
width: 150,
child: DropdownButton<String>(
value: selectedItem,
onChanged: (newValue) {
setState(() => selectedItem = newValue);
},
items: dropdownItems.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList()),
)
],
);
}
}
【问题讨论】:
-
你能显示 _dropDownWithLabel 小部件吗?
-
是的 - 我编辑了帖子以包含这个
标签: firebase flutter google-cloud-firestore