Search code examples
flutterdartgoogle-cloud-firestoredrop-down-menuflutter-layout

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

Image: enter image description here

enter image description here


Solution

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