如何将地图列表显示为扑朔迷离中的下拉式meNUITEM



我是新来的flutter。我想从我的列表变量显示下拉菜单。查看我的代码。


// 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>>>>'

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

尝试删除<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());

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

i使用YourClassView类型的下拉列表显示完整流程。我创建一个称为_listmenuitems的变量,该变量将保持您的下拉序列。您必须将其LT; dropdownmenuitem&gt;或铸造错误将发生。_currentComboview点可以使用户从下拉列表buttonfield选择的YourClassView对象保持。_loadStatusCombo从Initstate((覆盖都调用。它称为"遗漏",以获取称为DataViews的YourClassView对象列表。我通过施放和使用地图函数将数据维图映射到_listMenuitems中。确保包括.tolist以平整映射的结果。

YourClassView has two field: DisplayValue and DatabaseValue
YourClassView _currentComboView;
List<DropdownMenuItem> _listMenuItems =
  <DropdownMenuItem<YourClassView>>[];
                  DropdownButtonFormField<YourClassView>(
                      decoration: InputDecoration(
                          border: OutlineInputBorder(
                            borderRadius: const BorderRadius.all(
                              const Radius.circular(2.0),
                            ),
                          ),
                          filled: true,
                          hintStyle: TextStyle(color: Colors.grey[800]),
                          hintText: "Select a Status",
                          fillColor: Colors.orange),
                      items: _listMenuItems,
                      isDense: true,
                      isExpanded: true,
                      value: this._currentComboView,
                      validator: (value) =>
                          value == null ? 'field required' : null,
                      onChanged: (YourClassView value) {
                        setState(() {
                          this._currentComboView = value;
                        });
                      }),

_loadStatusCombo() {
    Provider.of<Api>(context, listen: false)
        .getComboViews()
        .then((dataViews) {
        setState(() {
          _listMenuItems =
             dataViews.map<DropdownMenuItem<YourClassView>>((item) {
            return DropdownMenuItem<YourClassView>(
                value: item, child: Text(item.displayValue));
        }).toList();
      });
     });
   }

最新更新