neena
neena

Reputation: 581

Error while loading List to DropDownList in flutter

  1. I have an Api response like

[{"Food_name":"value1"},{"Food_name":"value2"},{"Food_name":"value3"},{"Food_name":"value4"},{"Food_name":"value5"}]

I got the reponse in a List Food_Name, but when try to show this list in dropdown showing an error as type List is not a subtype of List>, may be I am using it wrong way. Can anyone help?

Upvotes: 0

Views: 49

Answers (2)

Subin S V
Subin S V

Reputation: 85

List cannot be directly supplied to items.

Items accept

List<DropdownMenuItem<T>> items

What you have is

List<Map<String,String>>
var jsonList = [
    {"Food_name": "value1"},
    {"Food_name": "value2"},
    {"Food_name": "value3"},
    {"Food_name": "value4"},
    {"Food_name": "value5"}
  ];

......
......



child: DropdownButton(
            onChanged: (value){

            },
            items: jsonList.map((Map<String, String> value) {
              return new DropdownMenuItem(
                value: value["Food_name"],
                child: Text(value["Food_name"]),
              );
            }).toList(),
          ),
        ),

Upvotes: 1

Doan Bui
Doan Bui

Reputation: 4418

I suggest you use : PopupMenuButton

Widget _typeTeachPopup() => PopupMenuButton<Food>(
    onSelected: _selectFood,
    child: ListTile(
      title: Padding(
        padding: const EdgeInsets.only(top: 0, left: 0, right: 0),
        child: Card(
          elevation: 5,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8)),
        ),
      ),
    ),
    itemBuilder: (BuildContext context) {
      return _listFoods.map((Food choice) {
        return PopupMenuItem<TypeTeach>(
          value: choice,
          child: Text(choice.food_name),
        );
      }).toList();
    },
  );

The result is as follows :

enter image description here

Upvotes: 0

Related Questions