动态下拉按钮,从Flutter中的列表获取动态值。

huangapple go评论55阅读模式
英文:

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&lt;String&gt; _values = [
    &quot;m\u00b3&quot;,
    &quot;ft\u00b3&quot;,
    &quot;cm\u00b3&quot;,
    &quot;m\u00b2&quot;,
    &quot;ft\u00b2&quot;,
    &quot;cm\u00b2&quot;,
    &quot;kg&quot;,
    &quot;lb&quot;,
    &quot;ton&quot;,
    &quot;No\&#39;s&quot;,
  ];
  List&lt;String&gt; _selectedValues = [];

here is the initState

  void initState() {
    super.initState();
    _selectedValues = List.generate(_values.length, (index) =&gt; _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) =&gt; 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&lt;String&gt; _values = [
    &quot;m\u00b3&quot;,
    &quot;ft\u00b3&quot;,
    &quot;cm\u00b3&quot;,
    &quot;m\u00b2&quot;,
    &quot;ft\u00b2&quot;,
    &quot;cm\u00b2&quot;,
    &quot;kg&quot;,
    &quot;lb&quot;,
    &quot;ton&quot;,
    &quot;No\&#39;s&quot;,
  ];
  List&lt;String&gt; _values2 = [
    &quot;m\u00b3&quot;,
    &quot;ft\u00b3&quot;,
    &quot;cm\u00b3&quot;,
    &quot;m\u00b2&quot;,
    &quot;ft\u00b2&quot;,
    &quot;cm\u00b2&quot;,
    &quot;kg&quot;,
    &quot;lb&quot;,
    &quot;ton&quot;,
    &quot;No\&#39;s&quot;,
  ];

the code

List.generate(
      _values2.length,
      (index) =&gt; DataRow(
        cells: [
          DataCell(
            Container(
              child: DropdownButton(
                value: _values2[index],
                items: _values
                    .map((value) =&gt; DropdownMenuItem(
                          value: value,
                          child: Text(value),
                        ))
                    .toList(),
                onChanged: (value) {
                  setState(() {
                    _values2[index] = value.toString();
                  });
                },
              ),
            ),
          ),
        ],
      ),
    );

huangapple
  • 本文由 发表于 2023年2月6日 05:05:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定