【问题标题】:Listview builder not updating values in listListview builder不更新列表中的值
【发布时间】:2020-06-12 18:14:51
【问题描述】:

我在 Stack 中有一个定位的 ListViewBuilder,试图在具有不同下拉值的多个图块中显示动态下拉列表。这似乎工作正常,除了当列表使用新值更新并在 SetState 上刷新时,这些值似乎没有正确清除。

ListView 构建器代码

Positioned(
              top: 100,
              left: 100,
              width: 90,
              height: MediaQuery.of(context).size.height,
              child: ListView.builder(
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                itemCount: dropDownListValues.length,
                itemBuilder: (context, index) {
                  return Tile(
                    tileId: dropDownListValues[index],
                    isHeaderTile: false,
                    uuid: Uuid().v1(),
                  );
                },
              ),
            ),

根据按下的按钮清除和更新列表。

  void _toggleDropDown(List valueList) {
    if (dropDownListValues.isEmpty) {
      setState(() {
        dropDownListValues = valueList;
      });
    } else {
      setState(() {
        dropDownListValues.clear();
      });
    }
  }

我最终得到的是列表根据下拉列表中的项目数进行扩展,但上一个列表中的值从最后一个列表中继承..

示例。

下拉列表值

Dropdown list 1 = ['one', 'two']
Dropdown list 2 = ['two', 'three', 'four']

我看到的是

Dropdown list 1 = ['one', 'two']
Dropdown list 2 = ['one', 'two', 'four']

如果我首先点击列表两个下拉列表,我会得到以下内容

Dropdown list 1 = ['two', 'three']
Dropdown list 2 = ['two', 'three', 'four']

我被难住了,花了好几个小时,知道我做错了什么导致这个刷新问题吗?

提前致谢。

【问题讨论】:

  • 你期待什么结果?
  • 我期待下拉菜单复制列表中的值。似乎已经被预览下拉列表使用的列表索引没有被更新,但是,如果新的下拉列表计数更高,它将用正确的值填充剩余的索引。这是一个奇怪的。

标签: flutter dart


【解决方案1】:

尝试使用keys,以便 Flutter 框架可以识别您的列表的更改。

【讨论】:

  • 感谢您的建议,但我在 StatefulWidget (Tile) 的构造函数中使用了键
  • 在您的样品上看不到。
  • 我正在使用它但不正确。现在正在运行,感谢您的建议
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2021-05-29
  • 2020-01-10
  • 1970-01-01
相关资源
最近更新 更多