【问题标题】:Populate dropdownbutton2 with data from json array in flutter在flutter中使用来自json数组的数据填充dropdownbutton2
【发布时间】:2022-07-26 23:28:04
【问题描述】:

如何使用 json 数组中的数据填充 dropdownbutton2 的数据这里是 Flutter 中的 json 示例

"data": [
        {
            "id": "1",
            "name": "Floral"
        },
        {
            "id": "4",
            "name": "Marigold"
        },
        {
            "id": "104",
            "name": "Tulip"
        }
    ]

如何让名称的值显示在下拉选择中,但 ID 将在 onchange 函数中选择

List dataList = [];

setState(() {
      dataList = jsonData;
    });

items: dataList
                      .map((item) => DropdownMenuItem<String>(
                            value: item,
                            child: Text(
                              item['name'],
                              style: const TextStyle(
                                fontSize: 14,
                                color: Colors.black,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ))
                      .toList(),
                  onChanged: (value) {
                    item['id'],
                 }),
``

【问题讨论】:

  • pub.dev/packages/dropdown_button2里确实有很好的例子。
  • 已经尝试过使用 json 数组数据填充
  • 你能附上你迄今为止尝试过的code-sn-p吗?

标签: arrays json flutter dart dropdownbutton


【解决方案1】:

从 json 中解析项目

final json = """{
  "data": [
        {
            "id": "1",
            "name": "Floral"
        },
        {
            "id": "4",
            "name": "Marigold"
        },
        {
            "id": "104",
            "name": "Tulip"
        }
    ]
}""";

final List<String> items = jsonDecode(json)['data'].map((i) => i['name']).toList().cast<String>();

并关注Simple DropdownButton2 示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 2020-11-09
    • 2018-10-13
    相关资源
    最近更新 更多