【发布时间】:2020-08-26 10:34:20
【问题描述】:
尽管这里有多个条目似乎有类似的问题,但我无法让它真正发挥作用。
我有两个依赖的 DropdownButtonFormFields 的设置,其中第二个在第一个更改后更改为另一个列表。
- 我能够将问题分解为第二个选择的选定值的持续剩余。我希望它会随着我在代码中提供的值信息而改变。
提供以下错误
════════ Exception caught by widgets library ═══════════════════════════════════
There should be exactly one item with [DropdownButton]'s value: GreenBananas.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
The relevant error-causing widget was
DropdownButtonFormField<String>
lib/…/testing/test.dart:242
══════════════════════════════════════════════════════════════════
我简化了示例并重建了错误,以便更好地分析问题并从你们那里获得更有价值的意见:)
class InputRowTest extends StatefulWidget {
@override
_InputRowTestState createState() => _InputRowTestState();
}
class _InputRowTestState extends State<InputRowTest> {
List<String> list1 = ['Apples', 'Bananas', 'Peaches'];
List<String> list1_1 = ['GreenApples', 'RedApples', 'YellowApples'];
List<String> list1_2 = [
'YellowBananas',
'BrownBananas',
'GreenBananas',
'GreenApples'
];
List<String> list1_3 = [
'RedPeaches',
'YellowPeaches',
'GreenPeaches',
'GreenApples'
];
List<String> _fromparent;
int _fromparentint;
//String selected;
@override
void initState() {
_fromparent = list1_1;
_fromparentint = 0;
//selected = list1[0];
super.initState();
}
@override
Widget build(BuildContext context) {
List<List<String>> subLists = [list1_1, list1_2, list1_3];
_fromparent = subLists[_fromparentint];
DropdownButtonFormField ddff = DropdownButtonFormField(
//value: selected, //list1[0],
//items: list1.map((category) {
value: _fromparent[0], //Seems this value wont change.
items: _fromparent.map((category) {
return DropdownMenuItem(
value: category,
child: Container(
child: Text(category),
),
);
}).toList(),
onChanged: (val) => print(val),
);
return Center(
child: Row(
children: <Widget>[
Expanded(
child: DropdownButtonFormField(
value: list1[0],
items: list1.map((category) {
return DropdownMenuItem(
value: category,
child: Container(
child: Text(category),
),
);
}).toList(),
onChanged: (val) {
setState(() {
//selected = val;
_fromparentint = list1.indexOf(val);
});
},
),
),
Expanded(
child: ddff,
),
],
),
);
}
}
【问题讨论】: