Flutter 颤振:带有自定义贴图的下拉菜单

Flutter 颤振:带有自定义贴图的下拉菜单,flutter,flutter-layout,Flutter,Flutter Layout,我有一个地图列表,列表如下: var taxesGroups = [ { "name": "Spain", "taxes": [ {"name": "IVA", "percentage": "21"}, {"name": "IRPF", "percentage": "

我有一个地图列表,列表如下:

var taxesGroups = [
{
  "name": "Spain",
  "taxes": [
    {"name": "IVA", "percentage": "21"},
    {"name": "IRPF", "percentage": "19"},
  ]
},
{
  "name": "UK",
  "taxes": [
    {"name": "VAT", "percentage": "20"},
  ]
}
];

var dropdownValue = taxesGroups[0]["name"];
到目前为止,我试过:

DropdownButton(
                  value: dropdownValue,
                  items: taxesGroups.map((taxGroup) {
                    return DropdownMenuItem<String>(
                      value: taxGroup["name"],
                      child: Text(taxGroup["name"]),
                    );
                  }).toList(),
                  onChanged: (taxGroup) {
                    setState(() {
                      dropdownValue = taxGroup;
                    });
                  },
                ),
下拉按钮(
value:dropdownValue,
项目:taxesGroups.map((taxGroup){
返回下拉菜单项(
值:taxGroup[“名称”],
子项:文本(分类组[“名称]),
);
}).toList(),
变更后:(税务组){
设置状态(){
dropdownValue=分类组;
});
},
),
我得到这个错误:

类型“List”不是类型的子类型 “列表”


我想这与我想在下拉列表中显示的信息(西班牙、英国)以及我在选择一个选项时得到的信息有关,但我没有弄清楚,因为您与税务组地图不匹配

我试着用这个重新创建你的用例,它成功了

            class MyApp2 extends StatefulWidget {
              @override
              _MyAppState createState() => _MyAppState();
            }

            class _MyAppState extends State<MyApp2> {
              @override
              Widget build(BuildContext context) {
                return MaterialApp(
                  home: MyHomePage(),
                );
              }
            }

            class MyHomePage extends StatefulWidget {
              @override
              _MyHomePageState createState() => _MyHomePageState();
            }

            class _MyHomePageState extends State<MyHomePage> {
              var taxesGroups = [
                {
                  "name": "Spain",
                  "taxes": [
                    {"name": "IVA", "percentage": "21"},
                    {"name": "IRPF", "percentage": "19"},
                  ]
                },
                {
                  "name": "UK",
                  "taxes": [
                    {"name": "VAT", "percentage": "20"},
                  ]
                }
              ];

              var dropdownValue;

              @override
              void initState() {
                dropdownValue = taxesGroups[0];
                super.initState();
              }

              @override
              Widget build(BuildContext context) {
                return Scaffold(
                  appBar: AppBar(
                    title: Text("AppBar"),
                  ),
                  body: DropdownButton(
                    value: dropdownValue['name'],
                    items: taxesGroups.map((taxGroup) {
                      return DropdownMenuItem<String>(
                        value: taxGroup["name"],
                        child: Text(taxGroup["name"]),
                      );
                    }).toList(),
                    onChanged: (taxGroup) {
                      print('taxGroup $taxGroup');
                      taxesGroups.map((e) {
                        if (e["name"] == taxGroup)
                          setState(() {
                            dropdownValue = e;
                          });
                      });
                    },
                  ),
                );
              }
            }
类MyApp2扩展了StatefulWidget{ @凌驾 _MyAppState createState()=>\u MyAppState(); } 类MyAppState扩展了状态{ @凌驾 小部件构建(构建上下文){ 返回材料PP( 主页:MyHomePage(), ); } } 类MyHomePage扩展StatefulWidget{ @凌驾 _MyHomePageState createState()=>\u MyHomePageState(); } 类_MyHomePageState扩展状态{ var税组=[ { “名称”:“西班牙”, “税收”:[ {“名称”:“IVA”,“百分比”:“21”}, {“名称”:“IRPF”,“百分比”:“19”}, ] }, { “名称”:“英国”, “税收”:[ {“名称”:“增值税”,“百分比”:“20”}, ] } ]; var下降值; @凌驾 void initState(){ dropdownValue=TaxesGroup[0]; super.initState(); } @凌驾 小部件构建(构建上下文){ 返回脚手架( appBar:appBar( 标题:文本(“AppBar”), ), 正文:下拉按钮( 值:dropdownValue['name'], 项目:taxesGroups.map((taxGroup){ 返回下拉菜单项( 值:taxGroup[“名称”], 子项:文本(分类组[“名称]), ); }).toList(), 变更后:(税务组){ 打印('taxGroup$taxGroup'); taxesGroups.map((e){ 如果(e[“名称”]==taxGroup) 设置状态(){ 下拉值=e; }); }); }, ), ); } }
将类型添加为地图的DropownMenuItem

    DropdownButton(
     value: dropdownValue,
     items: taxesGroups.map<DropdownMenuItem>((taxGroup) {
      return DropdownMenuItem<String>(
         value: taxGroup["name"],
         child: Text(taxGroup["name"]),
       );
     }).toList(),
     onChanged: (taxGroup) {
      setState(() {
       dropdownValue = taxGroup;
      });
    },
   ),
下拉按钮(
value:dropdownValue,
项目:taxesGroups.map((taxGroup){
返回下拉菜单项(
值:taxGroup[“名称”],
子项:文本(分类组[“名称]),
);
}).toList(),
变更后:(税务组){
设置状态(){
dropdownValue=分类组;
});
},
),

我不知道为什么,但如果我不为.forEach更改内部的.map,这对我来说是行不通的。但是,是的,它起作用了。所以唯一的办法就是再做一次循环?我不能拿走我正在点击的地图并避开额外的地图?或者至少使用单击选项的索引来执行taxesGroups[index],如果您以某种方式获得了索引,那么是的,您可以这样做。但我不知道怎么做。这并不能解决任何问题,只是添加了一个类型