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?. 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.
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(),
);
},