简体   繁体   English

如何将 json 列表项添加到 Flutter 中的下拉搜索项 | Dart

[英]How to add json list items to dropdown search items in Flutter | Dart

I'm creating a dropdown button with search.我正在创建一个带有搜索功能的下拉按钮。 So I using this package. Here with this package I have problem.所以我使用这个package。这个 package 我有问题。 The response from the API is like this. API 的响应是这样的。

List countries = [
    {
      'id': '1',
      'name': 'Brazil',
    },
    {
      'id': '2',
      'name': 'India',
    },
    {
      'id': '3',
      'name': 'Japan',
    },
    {
      'id': '4',
      'name': 'Tokyo',
    },
    {
      'id': '5',
      'name': 'Australia',
    },
    {
      'id': '6',
      'name': 'Srilanka',
    },
    {
      'id': '7',
      'name': 'Canada',
    },
  ];

My Dropdown code is this,我的下拉代码是这样的,

body: Column(
        children: [
          DropdownSearch<String>(
            mode: Mode.MENU,
            items: countries['name'],
            showSearchBox: true,
            label: "Menu mode",
            hint: "country in menu mode",
            onChanged: (value){
               print(countries['name']);
            },
            selectedItem: "Canada",
          ),
        ],
      ),

Here items: countries['name'] line i getting error as The argument type 'String' can't be assigned to the parameter type 'int'.此处items: countries['name']行我收到错误,因为The argument type 'String' can't be assigned to the parameter type 'int'. Also when i select a country i want to print the country id.另外,当我 select 一个国家时,我想打印国家 ID。 eg: if i select country as japan then it should print 4 in console.例如:如果我 select country as japan 那么它应该在控制台中打印 4。 My Code is not working.我的代码不工作。

First of all, a List of Map doesn't work like this -> ( countries['name] ), try replace your items to:首先,Map 的列表不能像这样工作->( countries['name] ),尝试将您的items替换为:

DropdownSearch<String>(
        mode: Mode.MENU,
        items: countries.map((e)=>e['name']).toList(),
        showSearchBox: true,
        label: "Menu mode",
        hint: "country in menu mode",
        onChanged: (value){
           print(value);
        },
        selectedItem: "Canada",
      ),

Try below code hope its help to you.试试下面的代码希望它对你有帮助。

Refer my answer here for put json data to dropdown请参阅我的答案,将json数据放入下拉列表

     Padding(
              padding: const EdgeInsets.all(8.0),
              child: DropdownSearch<dynamic>(
                mode: Mode.MENU,
                items: countries.map((e) => e['name']).toList(),
                showSearchBox: true,
                label: "Menu mode",
                hint: "country in menu mode",
                onChanged: (value) {},
                selectedItem: "Canada",
              ),
            ),

Your result screen->您的结果屏幕-> 在此处输入图像描述

you have to create a data class Country and override the toString function to return the country name.您必须创建数据 class Country 并覆盖 toString function 以返回国家名称。

and update your dropdownSearch like this:并像这样更新您的下拉搜索:

//don't forget to generate the list of countries frop the map

DropdownSearch<Country>(
        mode: Mode.MENU,
        items: yourCountries,
        showSearchBox: true,
        label: "Menu mode",
        hint: "country in menu mode",
        onChanged: (Country value){
           print(value.id);
        },
        selectedItem: Country("Canada",7),
      ),

use your own country Model like this像这样使用你自己的国家 Model

DropdownSearch<Country>(
        mode: Mode.MENU,
        items: yourCountries,
        popupProps: PopupProps.dialog(
                     showSelectedItems: false,
                     showSearchBox: true,
                    ),
        showSearchBox: true,
        label: "Menu mode",
        hint: "country in menu mode",
        itemAsString: (Country u) => u.name,
        onChanged: (Country country){
           print(country.name);
           print(country.id);
        },
        selectedItem: Country("Canada",7),
      ),

don't forget to use -> itemAsString field不要忘记使用 -> itemAsString field

//OR //或者

you can Visit the - https://pub.dev/packages/dropdown_search#customize-showed-field-itemasstring .您可以访问 - https://pub.dev/packages/dropdown_search#customize-showed-field-itemasstring section from the package it self部分来自 package it self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM