JahidRatul
JahidRatul

Reputation: 519

How to get multiple values from DropdownButtonFormField in flutter?

selecting one item from dropdown menu, i want to show its multiple values inside textfields in flutter. For example, if I select school it will show its name, location, contact no. so i need to collect multiple values from that item. how can i have multiple values from dropDownMenuItem to show multiple values? for example School list item is given below:

      "SchoolList": [
        {
          "name": "school1",
          "location": "location1",
          "contact_no": "address1"
        },
         {
          "name": "school2",
          "location": "location2",
          "contact_no": "address2"
        },
        {
          "name": "school3",
          "location": "location3",
          "contact_no": "address3"
        },
        
      ],

Upvotes: 0

Views: 4286

Answers (1)

JahidRatul
JahidRatul

Reputation: 519

at first we need to pass Item type as parameter of DropdownButtonFormField. Items will be that type of list and it will return dropDownMenuItem of that type. Then we will assign values from items inside onChanged section.


     DropdownButtonHideUnderline(
                                        child: DropdownButtonFormField<
                                            SchoolList>(
                                          validator: (value) => value == null
                                              ? 'field required'
                                              : null,
                                          value: selectedItem,
                                          icon: const Icon(
                                            Icons.arrow_drop_down,
                                            color: Colors.grey,
                                          ),
                                          iconSize: 24,
                                          elevation: 16,
                                          hint: Text(
                                            'Select',
                                          ),
                                          onChanged:
                                              (SchoolList?
                                                  schoolList) {
                                            setState(() {
                                              
                                              selectedSchoolName =
                                                  schoolList!
                                                      .name;
                                              selectedSchoolLocation  =
                                                  schoolList!
                                                      .location;
                                              selectedSchoolContactNo                                            
                                                            =
                                                  schoolList!
                                                      .contact_no;
                                            });
                                          },
                                          //
                                          items: =
                                              SchoolList
                                              ?.map((item) {
                                            return DropdownMenuItem<
                                                SchoolList>(
                                              value: item,
                                              child:
                                                  Text(item.name)),
                                            );
                                          }).toList(),
                                        ),
                                      ),

Upvotes: 1

Related Questions