如何在Flutter中将地图列表显示到DropdownMenuItem中

时间:2019-06-25 11:07:13

标签: list flutter dart

我是Flutter的新手。 我想从列表变量中显示DropdownMenuItem。 查看我的代码。


// my list variable


  List listUserType = [
    {'name': 'Individual', 'value': 'individual'},
    {'name': 'Company', 'value': 'company'}
  ];




// items property in DropdownMenuItem

return DropdownButtonFormField<List<Map>>(
        decoration: InputDecoration(
          prefixIcon: Icon(Icons.settings),
          hintText: 'Organisation Type',
          filled: true,
          fillColor: Colors.white,
          errorStyle: TextStyle(color: Colors.yellow),
        ),
        items: listUserType.map((map) {
          return DropdownMenuItem(
            child: Text(map['name']),
            value: map['value'],
          );
        }).toList());

这是我得到的结果

I/flutter (22528): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (22528): The following assertion was thrown building RegisterPage(dirty, state: RegisterPageState#5b83a):
I/flutter (22528): type 'List<DropdownMenuItem<dynamic>>' is not a subtype of type
I/flutter (22528): 'List<DropdownMenuItem<List<Map<dynamic, dynamic>>>>'

我不知道是什么原因导致了错误。

2 个答案:

答案 0 :(得分:1)

尝试删除<List<Map>>

return DropdownButtonFormField(
        decoration: InputDecoration(
          prefixIcon: Icon(Icons.settings),
          hintText: 'Organisation Type',
          filled: true,
          fillColor: Colors.white,
          errorStyle: TextStyle(color: Colors.yellow),
        ),
        items: listUserType.map((map) {
          return DropdownMenuItem(
            child: Text(map['name']),
            value: map['value'],
          );
        }).toList());

答案 1 :(得分:0)

DropdownButtonFormField<List<Map>>更改为DropdownButtonFormField<String>,并将String类型的参数添加到return DropdownMenuItem<String>