[英]I want to display document ids of collection in future builder but i got error
Below is the code in which i want to display documents ids from cloud firestore but i got error.
下面是我想在其中显示来自云 Firestore 的文档 ID 的代码,但出现错误。 Error picture is also attached below
错误图片也附在下面
FutureBuilder<QuerySnapshot>(
future: db.collection('/Exam').get(),
builder:
(BuildContext context, AsyncSnapshot asyncSnapshot) {
if (asyncSnapshot.hasError)
return Text("Error: ${asyncSnapshot.error}");
if (!asyncSnapshot.hasData)
return const CircularProgressIndicator();
return DropdownButton<String>(
isExpanded: true,
items: asyncSnapshot.data!.docs
.map(
(snap) => DropdownMenuItem(
value: snap.id,
child: Text(
snap.id.toString(),
),
),
)
.toList(),
value: _selectedexam,
onChanged: (String? newValue) {
setState(() {
_selectedexam = newValue!;
_selectedsemester = null;
print(_selectedexam);
});
},
);
}),
class _MyHomePageState extends State<MyHomePage> {
final docs = ...
String selectedId;
@overrid
initState(){
super.initState()
selectedId = docs.first.id;
}
@override
Widget build(BuildContext context) {
final items = docs.map<DropdownMenuItem<String>>((Document value) {
return DropdownMenuItem<String>(
value: value.id,
child: Text(value.id),
);
}).toList();
return Container(
child: DropdownButton<String>(
value: selectedId,
onChanged: (String? value) {
setState(() {
selectedId = value!;
});
},
items: items,
)
),
);
}
}
I edited directly in stackoverflow editor, so I may have made typos.我直接在stackoverflow编辑器中编辑,所以我可能打错了。 Also consider using a FutureBuilder as suggested by @Jacob
还可以考虑使用 @Jacob 建议的 FutureBuilder
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.