首页 > 解决方案 > Listview builder不更新列表中的值

问题描述

我在 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']

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

提前致谢。

标签: flutterdart

解决方案


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


推荐阅读