我已经使用DropdownButton
和TextField
小部件编写了一个相当广泛的表单。概念是我有一个StatefulWidget
,其中State<StatefulWidget>
的类包含两个方法,它们返回我想要构建的小部件。这样,我就可以轻松地访问和使用输入的数据,并将其传递给函数以编写电子邮件。
但是,当我从选项中选择一个项目时,框架在重新构建过程中抛出异常。我添加了一些日志函数,结果显示setState()
方法成功地将值保存到selectedValue
变量。
Widget buildMultiChoiceInputRow(var label, List<String> values) {
final List<String> options = values.toList();
selection = options.first;
final dropDownMenuOptions = options.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList();
return new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Expanded(
child: new Container(
padding:
const EdgeInsets.only(left: 5.0, top: 2.0, right: 5.0),
child: new Text(label, style: commonInfoCardInfoTextBlack16Bold)),
),
],
),
new Row(
children: <Widget>[
new Expanded(
child: new Container(
padding: const EdgeInsets.only(left: 5.0, right: 5.0),
child: new DropdownButton(
value: selectedValue,
items: dropDownMenuOptions,
onChanged: (selection) {
setState(() {
selectedValue = selection;
switch (label) {
case labelVirtualAdoption:
tempAdoptionType =
composeMultiChoiceAnswer(label, selection);
print(selection);
print(selectedValue);
break;
case labelAskedAboutSpecies:
tempAskedAboutSpecies =
composeMultiChoiceAnswer(label, selection);
break;
case labelHouseOrFlat:
tempHouseOrFlat =
composeMultiChoiceAnswer(label, selection);
break;
....
default:
break;
}
});
}),
),
)
],
),
new Divider(color: Colors.transparent)
],
);
}
以下是例外情况:
I/flutter (20998): The following assertion was thrown building AdoptionInput(dirty, state: AdoptionInputState#3cc80):
I/flutter (20998): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 481 pos 15: 'value == null ||
I/flutter (20998): items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.
下面是堆栈,显示了在重建过程中抛出的异常:
I/flutter (20998): #2 new DropdownButton (package:flutter/src/material/dropdown.dart)
I/flutter (20998): #3 AdoptionInputState.buildMultiChoiceInputRow (package:osszefogasaszanhuzokert/adoptionPageUtilities.dart:443:28)
I/flutter (20998): #4 AdoptionInputState.build (package:osszefogasaszanhuzokert/adoptionPageUtilities.dart:639:11)
I/flutter (20998): #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:3730:27)
I/flutter (20998): #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3642:15)
I/flutter (20998): #7 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (20998): #8 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2242:33)
这个问题看起来与a former bug in flutter很相似,但是如果我尝试在initState()
中初始化selection
和selectedValue
,在第一次构建表单时将抛出相同的异常。
这里我漏掉了什么?
发布于 2018-08-10 08:23:27
您的DropdownButton的“值”应设置为“null”或值列表中的值。
DropdownButton(
value: null,
isDense: true,
onChanged: (String newValue) {
// somehow set here selected 'value' above whith
// newValue
// via setState or reactive.
},
items: ['yellow', 'brown', 'silver'].map((String value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList(),
),
因此,对于我的示例,DropdownButton值应该设置为空,或者是“黄色”、“棕色”或“银色”。
发布于 2019-02-03 08:34:24
将上面的答案扩展到我被卡住的第二种情况。
DropdownButton的“值”应设置为“null”或值列表中的值。
您的“values”在每个项目值中也应该不同。
例如:避免这种情况
items.add(DropdownMenuItem(
value: 1.toString(),
child: Text(1.toString()),
));
items.add(DropdownMenuItem(
value: 1.toString(),
child: Text(1.toString()),
));
避免重复这些值。
发布于 2020-01-29 17:08:56
使用var声明变量,而不是字符串。现在您不需要将缺省值设置为null。
var dropdownvalue;
DropdownButton<String>(
value: dropdownvalue,
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 28,
elevation: 20,
onChanged: (String newval){
setState((){
dropdownvalue = newval;
});
},
items: <String>["Registration","Verification", "ArenaRun"]
.map<DropdownMenuItem<String>>((String value){
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
https://stackoverflow.com/questions/50384426
复制相似问题