Flutter:应该只有一个项目具有 [DropdownButton] 的值:A
Flutter: There should be exactly one item with [DropdownButton]'s value: A
首先,我尝试先google但还是没有找到解决办法。另外,我尝试了通过谷歌搜索在 SO 上找到的许多选项,但仍然无法正常工作。所以,我开题了。
我的代码:
class AddRiderScreen extends StatefulWidget {
@override
_AddRiderScreenState createState() => _AddRiderScreenState();
}
class _AddRiderScreenState extends State<AddRiderScreen> {
String _riderNameSelected = '';
final List _listRiderName = [
'Personal Accident (Risk A)',
'Personal Accident (Risk B)',
'Personal Accident (Risk C)'
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Title'),
backgroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(16),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text('-- Pilih --'),
isExpanded: true,
value: _riderNameSelected,
items: _listRiderName.map((value) {
return DropdownMenuItem(
child: Text(value),
value: value,
);
}).toList(),
icon: Icon(
Icons.arrow_drop_down,
color: Colors.grey,
),
onChanged: (value) {
setState(() {
_riderNameSelected = value;
});
},
),
),
),
);
}
}
错误:
There should be exactly one item with [DropdownButton]'s value: A.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
有人可以知道这个问题并找到解决方法吗?因为我不知道如何解决这个问题。
只需删除 _riderNameSelected
中的 ''
。
String _riderNameSelected;
首先,我尝试先google但还是没有找到解决办法。另外,我尝试了通过谷歌搜索在 SO 上找到的许多选项,但仍然无法正常工作。所以,我开题了。
我的代码:
class AddRiderScreen extends StatefulWidget {
@override
_AddRiderScreenState createState() => _AddRiderScreenState();
}
class _AddRiderScreenState extends State<AddRiderScreen> {
String _riderNameSelected = '';
final List _listRiderName = [
'Personal Accident (Risk A)',
'Personal Accident (Risk B)',
'Personal Accident (Risk C)'
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Title'),
backgroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(16),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text('-- Pilih --'),
isExpanded: true,
value: _riderNameSelected,
items: _listRiderName.map((value) {
return DropdownMenuItem(
child: Text(value),
value: value,
);
}).toList(),
icon: Icon(
Icons.arrow_drop_down,
color: Colors.grey,
),
onChanged: (value) {
setState(() {
_riderNameSelected = value;
});
},
),
),
),
);
}
}
错误:
There should be exactly one item with [DropdownButton]'s value: A.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
有人可以知道这个问题并找到解决方法吗?因为我不知道如何解决这个问题。
只需删除 _riderNameSelected
中的 ''
。
String _riderNameSelected;