[英]How can I put a list of snapShot of String in array inside the DropdownButton flutter
I have a DropdownButton here and a snapshot of string But I want to put a list inside a DropdownMenuItem so I can select it later How can I do that?.我这里有一个 DropdownButton 和一个字符串快照,但我想在 DropdownMenuItem 中放置一个列表,这样我以后就可以 select 我该怎么做? code:代码:
String? selectedUserName;
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("groups")
.doc(groupId)
.snapshots(),
builder: (context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
//I want to put this snapshot inside the DropdownMenuItem
var userDocumentUid = snapshot.data?["members"]as List?;
if (!snapshot.hasData) {
return Container();
}
return DropdownButton(
hint: const Text("to"),
value: selectedUserName,
onChanged: (newValue) {
setState(() {
selectedUserName =
newValue as String?;
});
},
items: userDocumentUid!.map(
(userDocumentUid) {
return DropdownMenuItem<String?>(
value: userDocumentUid,
child: Text(userDocumentUid),
);
},
).toList(),
)
While you are receiving a list of string, try to add data type and map the list to return widget.当您收到字符串列表时,尝试添加数据类型和 map 列表以返回小部件。
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
//I want to put this snapshot inside the DropdownMenuItem
var userDocumentUid = snapshot.data?["members"] as List?;
if (!snapshot.hasData || userDocumentUid == null) {
return Container();
}
return DropdownButton<String?>(
hint: const Text("to"),
value: selectedUserName,
onChanged: (newValue) {
setState(() {
selectedUserName = newValue;
});
},
items: userDocumentUid.map(
(userDocumentUid) {
return DropdownMenuItem(
value: userDocumentUid,
child: Text(userDocumentUid),
);
},
).toList(),
);
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.