尽管这里有多个条目似乎有类似的问题,但我无法真正实现这一目标。
我有两个依赖的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,
),
],
),
);
}
}
答案 0 :(得分:0)
我找到了解决该问题的方法。
Flutter似乎没有重建DropDownFormField,而是认为完全可以保留它。在这种情况下,它甚至还很固执。
由于我找不到重建场的方法,因此我创建了一个非常讨厌但可以工作的目录。还需要打磨。
我基本上让flutter相信我每次都提供不同的小部件。
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;
Widget ddbff;
var selected;
bool chance;
Widget ddff(List<String> list, bool chance) {
return (chance)
? DropdownButtonFormField(
value: list[0], //Seems this value wont change.
items: list.map((category) {
return DropdownMenuItem(
value: category,
child: Container(
child: Text(category),
),
);
}).toList(),
onChanged: (val) {
print(val);
},
)
: Container(
child: DropdownButtonFormField(
value: list[0], //Seems this value wont change.
items: list.map((category) {
return DropdownMenuItem(
value: category,
child: Container(
child: Text(category),
),
);
}).toList(),
onChanged: (val) {
print(val);
},
),
);
}
@override
void initState() {
_fromparent = list1_1;
_fromparentint = 0;
selected = list1_1[0];
chance = true;
ddbff = ddff(_fromparent, chance);
super.initState();
}
@override
Widget build(BuildContext context) {
List<List<String>> subLists = [list1_1, list1_2, list1_3];
_fromparent = subLists[_fromparentint];
chance = !chance;
ddbff = ddff(_fromparent, chance);
return Center(
child: Container(
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(() {
_fromparentint = list1.indexOf(val);
});
},
),
),
Expanded(
child: ddbff,
),
],
),
),
);
}
}