我正在尝试获取一个Firebase集合中所有文档名称的列表,以便在一个DropdownButton中显示,为此我创建了一个自定义小部件_DropdownListWithLabel。我的集合中有两个文档(我可以在Firebase控制台中看到这两个文档),但是当我尝试以字符串列表的形式检索所有文档的名称时,只显示了一个。下面是执行此操作的代码片段:
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或列中列出文档名,结果与只显示一个文档名的结果相同。
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()),
)
],
);
}
}发布于 2019-09-06 21:59:11
我通过关闭模拟器并重新启动解决了我的问题。显然,有什么东西阻止了它正确地连接到Firestore。
https://stackoverflow.com/questions/57764780
复制相似问题