英文:
dynamic dropdown button with dynamic values from list in flutter
问题
我在Flutter中有一个动态创建的DataTable,其中有一列填充有下拉按钮,我遇到了这个"RangeError"错误。
以下是代码:
List<String> _values = [
"m³",
"ft³",
"cm³",
"m²",
"ft²",
"cm²",
"kg",
"lb",
"ton",
"No's",
];
List<String> _selectedValues = [];
void initState() {
super.initState();
_selectedValues = List.generate(_values.length, (index) => _values[0]);
}
这是我动态创建的数据单元格:
DataCell(
Container(
child: DropdownButton(
value: _selectedValues[index],
items: _values
.map((value) => DropdownMenuItem(
value: value,
child: Text(value),
))
.toList(),
onChanged: (value) {
setState(() {
_selectedValues[index] = value!;
});
},
),
),
)
以下是我遇到的错误:
异常已发生。
RangeError(RangeError(索引):无效值:不在包含范围0..9中:10)
如有任何帮助将不胜感激。
英文:
I have DataTable in flutter, which is dynamically created. I have one column which is filled with dropdown buttons, I am facing this out of Range Error.
Here is the code
List<String> _values = [
"m\u00b3",
"ft\u00b3",
"cm\u00b3",
"m\u00b2",
"ft\u00b2",
"cm\u00b2",
"kg",
"lb",
"ton",
"No\'s",
];
List<String> _selectedValues = [];
here is the initState
void initState() {
super.initState();
_selectedValues = List.generate(_values.length, (index) => _values[0]);
}
Here is the data cell in which I am creating it dynamically,
DataCell(
Container(
child: DropdownButton(
value: _selectedValues[index],
items: _values
.map((value) => DropdownMenuItem(
value: value,
child: Text(value),
))
.toList(),
onChanged: (value) {
setState(() {
_selectedValues[index] = value!;
});
},
),)),
Here is the Error I am Getting,
Exception has occurred.
RangeError (RangeError (index): Invalid value: Not in inclusive range 0..9: 10)
any help is highly appreciated.
答案1
得分: 0
我已添加了一个List.generate,所以所有下拉菜单的初始值将不同,而且如果更改一个下拉菜单的值,不会影响其他下拉菜单。
List<String> _values = [
"m³",
"ft³",
"cm³",
"m²",
"ft²",
"cm²",
"kg",
"lb",
"ton",
"No's",
];
List<String> _values2 = [
"m³",
"ft³",
"cm³",
"m²",
"ft²",
"cm²",
"kg",
"lb",
"ton",
"No's",
];
List.generate(
_values2.length,
(index) => DataRow(
cells: [
DataCell(
Container(
child: DropdownButton(
value: _values2[index],
items: _values
.map((value) => DropdownMenuItem(
value: value,
child: Text(value),
))
.toList(),
onChanged: (value) {
setState(() {
_values2[index] = value.toString();
});
},
),
),
),
],
),
);
英文:
I have added a List.genrate , so all the dropdown initial value would be different and also If you change value of one drop down it wont affect others
List<String> _values = [
"m\u00b3",
"ft\u00b3",
"cm\u00b3",
"m\u00b2",
"ft\u00b2",
"cm\u00b2",
"kg",
"lb",
"ton",
"No\'s",
];
List<String> _values2 = [
"m\u00b3",
"ft\u00b3",
"cm\u00b3",
"m\u00b2",
"ft\u00b2",
"cm\u00b2",
"kg",
"lb",
"ton",
"No\'s",
];
the code
List.generate(
_values2.length,
(index) => DataRow(
cells: [
DataCell(
Container(
child: DropdownButton(
value: _values2[index],
items: _values
.map((value) => DropdownMenuItem(
value: value,
child: Text(value),
))
.toList(),
onChanged: (value) {
setState(() {
_values2[index] = value.toString();
});
},
),
),
),
],
),
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论