相关疑难解决方法(0)

如何在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(),
Run Code Online (Sandbox Code Playgroud)

这是我的项目清单:

List<String> _locations = ['A', 'B', 'C', 'D'];
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

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.
Run Code Online (Sandbox Code Playgroud)

我假设值为_selectedLocationnull.但我正在初始化它.

String _selectedLocation = 'Please choose a location';

dart drop-down-menu flutter

33
推荐指数
9
解决办法
5万
查看次数

标签 统计

dart ×1

drop-down-menu ×1

flutter ×1