我有下面显示的这个小部件,只要项目是字母就可以工作,但是一旦我将它们切换为单引号中的数字,它就会停止工作并给我这个错误:
I/flutter (31770): The following assertion was thrown
building ScopedModelDescendant<MainModel>(dirty):
I/flutter (31770): 'package:flutter/src/material/dropdown.dart': Failed
assertion: line 481 pos 15: 'value == null ||
I/flutter (31770): items.where((DropdownMenuItem<T> item) => item.value
== value).length == 1': is not true.
这是实际的小部件:
Widget _buildServingDropdownButton(Product product) {
if (product != null) {
topperValue = product.topper;
}
return Padding(
padding: const EdgeInsets.all(0.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
ListTile(
title: const Text('Topper'),
trailing: DropdownButton<String>(
hint: Text('Choose'),
onChanged: (String value) {
setState(() {
topperValue = value;
print(topperValue);
});
},
value: topperValue,
items: <String>[
'1',
].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList()),
),
],
),
);
}
最佳答案
当指定的值在您的 DropdownMenuItem
中不可用时,会发生此错误。
你刚刚定义了一个项目:
items: <String>[
'1',
]
但如果您的 topperValue 不同,您将收到该错误。
if (product != null) {
topperValue = product.topper; //different from '1'
}
value: topperValue,
关于drop-down-menu - flutter 中的 DropdownButton 小部件不接受报价中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51751343/