小编典典

如何在flutter中实现下拉列表?

flutter

我有一个要在Flutter中实现为下拉列表的位置列表。我是该语言的新手。这是我所做的。

new DropdownButton(
  value: _selectedLocation,
  onChanged: (String newValue) {
    setState(() {
      _selectedLocation = newValue;
     });
},
items: _locations.map((String location) {
  return new DropdownMenuItem<String>(
     child: new Text(location),
  );
}).toList(),

这是我的物品清单:

List<String> _locations = ['A', 'B', 'C', 'D'];

而且我收到以下错误。

Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 468 pos 15: 'value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

我假设的值_selectedLocation将为空。但是我正在这样初始化它。

String _selectedLocation = 'Please choose a location';


阅读 2076

收藏
2020-08-13

共1个答案

小编典典

试试这个

new DropdownButton<String>(
  items: <String>['A', 'B', 'C', 'D'].map((String value) {
    return new DropdownMenuItem<String>(
      value: value,
      child: new Text(value),
    );
  }).toList(),
  onChanged: (_) {},
)
2020-08-13