我有一个要在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将为空。但是我正在这样初始化它。
_selectedLocation
String _selectedLocation = 'Please choose a location';
试试这个
new DropdownButton<String>( items: <String>['A', 'B', 'C', 'D'].map((String value) { return new DropdownMenuItem<String>( value: value, child: new Text(value), ); }).toList(), onChanged: (_) {}, )